pdf if문 문제풀이가 어렵다면 책이나 웹에 있는 if문 예제를 여러개 실행해보고 문제를 천천히 풀어 보고 해결이 안되면 답안을 보고 풀어 보거나 gpt를 이용해 보자.
java.util.Scanner sc = new java.util.Scanner(System.in);
int input = Integer.parseInt(sc.nextLine());
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
if(input == 0){
a = 5;
} else {
b = a + 3;
}
System.out.println(a);
System.out.println(b);
``` // input이 0일때 5 (b에 입력된 값)
// input이 0이 아닐때 (a에 입력된 값) ((a에 입력된 값) + 3)
2. 입력 받은 숫자의 절대값을 출력하는 프로그램을 만들어 보자.
(힌트: 0보다 작으면 -1를 곱하면 양수가 된다.)
```java
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
if(a < 0){
a *= -1;
}
System.out.println(a);
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
System.out.println((126 % a) == 0 ? "약수임" : "약수가 아님");
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
System.out.println((b % a) == 0 ? "약수임" : "약수가 아님");
java.util.Scanner sc = new java.util.Scanner(System.in);
int kor = Integer.parseInt(sc.nextLine());
int eng = Integer.parseInt(sc.nextLine());
int math = Integer.parseInt(sc.nextLine());
System.out.println(((kor + eng + math) / 3) > 80 ? "합격" : "불합격");
java.util.Scanner sc = new java.util.Scanner(System.in);
int x = Integer.parseInt(sc.nextLine());
if(x <= 5){
System.out.println(x + 15);
} else {
System.out.println(x + 5);
}
2번
4번
int a = 20;
int b = 0;
if(a > 10){
a = b;
} else {
b = a;
}
System.out.println(a);
System.out.println(b);
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
System.out.println("입력한 수는 10보다 " + (a > 10 ? "큽니다." : "크지 않습니다."))
java.util.Scanner sc = new java.util.Scanner(System.in);
int result = Integer.parseInt(sc.nextLine());
if(result == 0){
result = 0;
} else {
result = 1;
}
System.out.println(result);
java.util.Scanner sc = new java.util.Scanner(System.in);
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
int result = 0;
if(a > b){
result = a - b;
} else {
result = b - a;
}
System.out.println(result);
java.util.Scanner sc = new java.util.Scanner(System.in);
int big = 0;
int a = Integer.parseInt(sc.nextLine());
int b = Integer.parseInt(sc.nextLine());
int c = Integer.parseInt(sc.nextLine());
if(a > b){
big = a;
} else {
big = b;
}
if(big < c){
big = c;
}
System.out.println(big);
답안 3번
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("약수를 판별할 수를 입력하세요: ");
String input = scanner.nextLine();
int num = Integer.parseInt(input);
if (126 % num == 0) {
System.out.println(num + "은/는 126의 약수입니다.");
} else {
System.out.println(num + "은/는 126의 약수가 아닙니다.");
}
scanner.close();
}
}
답안 10번.
Scanner scanner = new Scanner(System.in);
System.out.print("숫자를 입력하세요: ");
String input = scanner.nextLine();
int num = Integer.parseInt(input);
int result;
if (num == 0) {
result = 0;
} else {
result = 1;
}
System.out.println(result);
scanner.close();