JAVA/문제풀이
Chapter 03. 연산자 문제 (홀수짝수출력, 몫과나머지,메트로폴리스,윤년)
inderrom
2022. 9. 11. 02:57
문제 1
package sec03.exam01;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("정수를 입력하세요 >> ");
int x = sc.nextInt();
System.out.println(x % 2 == 0 ? "짝수" : "홀수");
}
}
문제 2
(삼항연산자를 사용하여)
package sec03.exam01;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("첫 번째 숫자를 입력하세요: ");
int x =Integer.parseInt(sc.nextLine());
System.out.println("두 번째 숫자를 입력하세요: ");
int y = Integer.parseInt(sc.nextLine());
int big = (x > y) ? x : y;
int small = (big == x) ? y : x;
System.out.printf("큰 수를 작은 수로 나눈 몫은 %d이고 나머지는 %d이다.", big / small , big % small);
}
}
문제 3
package sec03.exam01;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("수도입니까?(수도: 1, 수도아님: 0)");
int input01 = Integer.parseInt(sc.nextLine());
System.out.println("총 인구는?(단위: 만)");
int input02 = Integer.parseInt(sc.nextLine());
System.out.println("연소득이 1억 이상인 인구는?(단위: 만)");
int input03 = Integer.parseInt(sc.nextLine());
boolean a = input01 == 1 && input02 > 99;
boolean b = input03 >49;
System.out.println(a||b ? "메트로폴리스" : "그냥도시" );
// if문을 써보면
if(input01 == 1 && input02 >99 || input03 > 49) {
System.out.println("이 도시는 메트로폴리스입니다");
}else {
System.out.println("도시아님");
}
}
문제 4
package sec03.exam01;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("윤년인지를 확인할 연도를 입력하세요: ");
int input = Integer.parseInt(sc.nextLine());
boolean a = input % 400 == 0;
boolean b = input % 4 == 0 && input % 100 != 0;
System.out.println(a||b ? input + "년은 윤년입니" : input + "년은 윤년이 아닙니다");
}
}
boolean 쓰는 법 잊지 말기!