import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
int e=24, o=3;
e=N%2;
o=N%2;
if(e==0)
{
System.out.println("Not Weird");
}
else
{
System.out.println("Weird");
}
}
}
n이 3일때 else문이 실행되어 Weird
n이 24일때 e==0이 실행되어 Not Weird
하지만 18일때 20일때 오류가 발생한다.
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if(N%2 == 0) { //짝수인경우
if(N >=2 && N <= 5) {
System.out.println("Not Weird");
} else if (N >=6 && N <= 20){
System.out.println("Weird");
}else if (N > 20){
System.out.println("Not Weird");
}
} else {
System.out.println("Weird");
}
scanner.close();
}
}
if문을 사용하여 문제를 해결하였다.
중간에 실수를 하나 했었다. 바로 조건문 괄호 뒤에 세미콜론을 붙인것.. ㅋㅋㅋ
else if (N > 20);
그렇게 하면 오류가 발생했다.해당 조건식이 참일 경우에도 항상 마지막의
System.out.println("Not Weird");이 실행되었기 때문에..
4일때 Not Weird, 18일때 Weird, 20일때 Weird
아래의 코드도 가능
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (N % 2 != 0) { //홀수인경우
System.out.println("Weird");
} else { //짝수인경우
if (N >= 2 && N <= 5 || N > 20) {
System.out.println("Not Weird");
} else if (N >= 6 && N <= 20) {
System.out.println("Weird");
}
}
scanner.close();
}
}
여담) Java 8로 풀고있는데 Java 15는 형식이 조금 달랐다. 그래도 7보다 높은 버전으로 사용하면 좋을듯하다!
'java' 카테고리의 다른 글
form에서 받은 값을 controller로 전달하기 (0) | 2023.09.27 |
---|---|
HackerRank 4단계 (0) | 2023.05.09 |
catch TypeError: Cannot read properties of undefined 해결하기 (0) | 2023.04.28 |
11-2~12. 입력과 출력, 직접 컴파일 (0) | 2023.01.23 |
쉽게배우는 자바 - 11-1 입력과 출력 (0) | 2023.01.23 |