
✅ 문제
시험 점수를 입력받아 90 ~ 100점은 A, 80 ~ 89점은 B, 70 ~ 79점은 C, 60 ~ 69점은 D, 나머지 점수는 F를 출력하는 프로그램을 작성하시오. 정수 A와 B가 주어졌을 때, A와 B를 비교하는 프로그램을 작성하시오.
조건문을 이해하기 위한 문제
📥 입력
첫째 줄에 시험 점수가 주어진다. 시험 점수는 0보다 크거나 같고, 100보다 작거나 같은 정수이다.
📥 출력
시험 성적을 출력한다.
● 90점 ~ 100점 : A
● 80점 ~ 89점 : B
● 70점 ~ 79점 : C
● 60점 ~ 69점 : D
● 그 외 : F
💡 풀이
Scanner 로 입력받아서 연산하는 법과 BufferedReader 로 입력받아서 연산하는 방법
두 가지로 문제를 해결하였다.
.
✅ 1. Scanner로 입력 받는 풀이
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
sc.close();
if (score>=90) {
System.out.println("A");
} else if (score>=80) {
System.out.println("B");
} else if (score>=70) {
System.out.println("C");
} else if (score>=60) {
System.out.println("D");
} else {
System.out.println("F");
}
}
}
if (조건) 뒤에 중괄호를 써서 블록을 지정해도 되지만 코드를 줄이고 싶으면 아래처럼 중괄호를 생략하고 바로 출력문을 적어도 된다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
sc.close();
if(A>=90) System.out.println("A");
else if(A>=80) System.out.println("B");
else if (A>=70) System.out.println("C");
else if(A>=60) System.out.println("D");
else System.out.println("F");
}
}
코드를 더 간단하게 줄이고 싶으면 아래처럼 삼항연산자를 쓴다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
sc.close();
System.out.print((A>=90)?"A": (A>=80)? "B": (A>=70)? "C": (A>=60)? "D": "F");
}
}
삼항 연산자(ternary operator)

int num1 = 5, num2 = 7;
int result;
result = (num1 - num2 > 0) ? num1 : num2;
System.out.println("두 정수 중 더 큰 수는 " + result + "입니다.");
[실행결과]
두 정수 중 더 큰 수는 7입니다.
✅ 2. BufferedReader로 입력받기
Scanner와 달리 BufferedReader 은 String 타입으로 받기 때문에 정수형을 입력받는 경우Integer.parseInt() 를 써서 int 형으로 변환시켜줘야 하는 과정이 필요하다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int A = Integer.parseInt(br.readLine());
if(A>=90) System.out.println("A");
else if(A>=80) System.out.println("B");
else if (A>=70) System.out.println("C");
else if(A>=60) System.out.println("D");
else System.out.println("F");
}
}
'BaekJoon' 카테고리의 다른 글
[백준] 14681번 사분면 고르기 - 자바 / java (1) | 2024.04.04 |
---|---|
[백준] 2753번 윤년 - 자바 / java (0) | 2024.04.04 |
[백준] 1330번 두 수 비교하기 - 자바 / java (2) | 2024.04.02 |
[백준] 10172번 개 - 자바/java (0) | 2024.04.02 |
[백준] 10171번 고양이 - 자바/java (0) | 2024.04.02 |

✅ 문제
시험 점수를 입력받아 90 ~ 100점은 A, 80 ~ 89점은 B, 70 ~ 79점은 C, 60 ~ 69점은 D, 나머지 점수는 F를 출력하는 프로그램을 작성하시오. 정수 A와 B가 주어졌을 때, A와 B를 비교하는 프로그램을 작성하시오.
조건문을 이해하기 위한 문제
📥 입력
첫째 줄에 시험 점수가 주어진다. 시험 점수는 0보다 크거나 같고, 100보다 작거나 같은 정수이다.
📥 출력
시험 성적을 출력한다.
● 90점 ~ 100점 : A
● 80점 ~ 89점 : B
● 70점 ~ 79점 : C
● 60점 ~ 69점 : D
● 그 외 : F
💡 풀이
Scanner 로 입력받아서 연산하는 법과 BufferedReader 로 입력받아서 연산하는 방법
두 가지로 문제를 해결하였다.
.
✅ 1. Scanner로 입력 받는 풀이
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
sc.close();
if (score>=90) {
System.out.println("A");
} else if (score>=80) {
System.out.println("B");
} else if (score>=70) {
System.out.println("C");
} else if (score>=60) {
System.out.println("D");
} else {
System.out.println("F");
}
}
}
if (조건) 뒤에 중괄호를 써서 블록을 지정해도 되지만 코드를 줄이고 싶으면 아래처럼 중괄호를 생략하고 바로 출력문을 적어도 된다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
sc.close();
if(A>=90) System.out.println("A");
else if(A>=80) System.out.println("B");
else if (A>=70) System.out.println("C");
else if(A>=60) System.out.println("D");
else System.out.println("F");
}
}
코드를 더 간단하게 줄이고 싶으면 아래처럼 삼항연산자를 쓴다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
sc.close();
System.out.print((A>=90)?"A": (A>=80)? "B": (A>=70)? "C": (A>=60)? "D": "F");
}
}
삼항 연산자(ternary operator)

int num1 = 5, num2 = 7; int result; result = (num1 - num2 > 0) ? num1 : num2; System.out.println("두 정수 중 더 큰 수는 " + result + "입니다.");
[실행결과]
두 정수 중 더 큰 수는 7입니다.
✅ 2. BufferedReader로 입력받기
Scanner와 달리 BufferedReader 은 String 타입으로 받기 때문에 정수형을 입력받는 경우Integer.parseInt() 를 써서 int 형으로 변환시켜줘야 하는 과정이 필요하다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int A = Integer.parseInt(br.readLine());
if(A>=90) System.out.println("A");
else if(A>=80) System.out.println("B");
else if (A>=70) System.out.println("C");
else if(A>=60) System.out.println("D");
else System.out.println("F");
}
}
'BaekJoon' 카테고리의 다른 글
[백준] 14681번 사분면 고르기 - 자바 / java (1) | 2024.04.04 |
---|---|
[백준] 2753번 윤년 - 자바 / java (0) | 2024.04.04 |
[백준] 1330번 두 수 비교하기 - 자바 / java (2) | 2024.04.02 |
[백준] 10172번 개 - 자바/java (0) | 2024.04.02 |
[백준] 10171번 고양이 - 자바/java (0) | 2024.04.02 |