#3 Java If-Else

HackerRank/HR-Java 2015. 11. 17. 00:23 by 후뤼한잉여

https://www.hackerrank.com/challenges/java-if-else


문제)

Given an integer N as input, check the following:

  • If N is odd, print "Weird".
  • If N is even and, in between the range of 2 and 5(inclusive), print "Not Weird".
  • If N is even and, in between the range of 6 and 20(inclusive), print "Weird".
  • If N is even and N>20, print "Not Weird".

We given you partially completed code in the editor, complete it to solve the problem.

Input Format

There is a single line of input: integer N.

Constraints 
1N100

Output Format

Print "Weird" if the number is weird. Otherwise, print "Not Weird". Do not print the quotation marks.


입력받은 N이 홀수이면 "Weird" 출력

N이 짝수면서 2~5 사이면 "Not Weird" 출력

N이 짝수면서 6~20 사이면 "Weird" 출력

N이 짝수면서 20보다 크면 "Not Weird" 출력


기본적으로 import 되어있는 라이브러리를 보면 정규식등 이용할 수 있게 한거 같은데 잘 모르니 무식하게 고


보낸 답)

  import java.util.Scanner;


    public class Solution {

        public static void main(String[] args) {


            Scanner sc = new Scanner(System.in);

            int n = sc.nextInt();

            String ans = "";

            

            if(n%2 == 1) {

                ans = "Weird";

            } else if(n > 20) {

                ans = "Not Weird";

            } else if(n > 6) {

                ans = "Weird";

            } else {

                ans = "Not Weird";

            }

            

            System.out.println(ans);

        }

    }


'HackerRank > HR-Java' 카테고리의 다른 글

#6 Java Loops  (0) 2015.11.18
#5 Java Output Formatting  (0) 2015.11.17
#4 Java Stdin and Stdout 2  (0) 2015.11.17
#2 Java Stdin and Stdout 1  (0) 2015.11.17
#1 Welcome to Java!  (0) 2015.11.17
Nav