728x90
세 수를 입력해서 평균60점 합격, 한과목 40점 미만 과락
3과목(a,b,c)의 점수를 입력받아서 합격인지 불합격인지 출력하시오
합격은 평균이 60점 이상이어야 하고 각 과목이 40점 이상이어야 한다
[실행결과]
a : 98
b : 90
c : 85
합격
a : 98
b : 90
c : 35
과락으로 불합격
a : 68
b : 50
c : 45
불합격
1.BufferedReader사용
package if_Ex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IfTest02_1 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a, b, c;
System.out.print("a : ");
a = Integer.parseInt(br.readLine());
//br.readLine이 String으로 받는거라서 Integer로 타입을 변환해야 한다.
System.out.print("b : ");
b = Integer.parseInt(br.readLine());
System.out.print("c : ");
c = Integer.parseInt(br.readLine());
int sum = a+b+c;
if(a>=40 & b>=40 & c>=40) {
if(sum/3 >=60) {
System.out.println("합격");
}else {
System.out.println("불합격");
}
}else {
System.out.println("과락으로 불합격");
}
}
}
2. Scanner사용
package if_Ex;
import java.util.Scanner;
public class IfTest02 {
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
int a, b, c;
System.out.print("a : ");
a = sc.nextInt();
//a = br.readLine();
System.out.print("b : ");
b = sc.nextInt();
//b = br.readLine();
System.out.print("c : ");
c = sc.nextInt();
//c = br.readLine();
int sum = a+b+c;
if(a>=40 & b>=40 & c>=40) {
if(sum/3 >=60) {
System.out.println("합격");
}else {
System.out.println("불합격");
}
}else {
System.out.println("과락으로 불합격");
}
}
}
3수를 입력받아 작은것 부터 나열하기
내가한 방법
package if_Ex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IfTest03 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a, b, c;
System.out.print("a : ");
a = Integer.parseInt(br.readLine());
System.out.print("b : ");
b = Integer.parseInt(br.readLine());
System.out.print("c : ");
c = Integer.parseInt(br.readLine());
if(a>b && a>c) { //a가 가장 크다면
if(b>c) {
System.out.println(c+","+b+","+a);
}else {
System.out.println(b+","+c+","+a);
}
}else if(b>c) { //그게아니라 만약에 b가 가장 크다면? b>a & b>c 할필요없음
if(a>c) {
System.out.println(c+","+a+","+b);
}else {
System.out.println(a+","+c+","+b);
}
}else {//그게아니라 c가 가장 크다면? c>a && c>b 할필요없음
if(a>b) {
System.out.println(b+","+a+","+c);
}else {
System.out.println(a+","+b+","+c);
}
}
}
}
d다른 방법
package if_Ex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IfTest03 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a, b, c;
System.out.print("a : ");
a = Integer.parseInt(br.readLine());
System.out.print("b : ");
b = Integer.parseInt(br.readLine());
System.out.print("c : ");
c = Integer.parseInt(br.readLine());
if(a<b && a<c) {//a가 가장 작다면
if(b<c) {
System.out.println(a+", "+b+", "+c);
}else {
System.out.println(a+", "+c+", "+b);
}
}else if(b<c) {//b가 가장 작다면
if(a<c) {
System.out.println(b+", "+a+", "+c);
}else {
System.out.println(b+", "+c+", "+a);
}
}else {//c가 가장 작다면
if(a<b) {
System.out.println(c+", "+a+", "+b);
}else {
System.out.println(c+", "+b+", "+a);
}
}
}
}
728x90
'JAVA' 카테고리의 다른 글
for문 연습03 (0) | 2020.09.08 |
---|---|
if, switch 연습(성적구하기) (0) | 2020.09.08 |
컴퓨터와 가위 바위 보 (0) | 2020.09.08 |
입력하는방법(Scanner, BufferedReader) (0) | 2020.09.04 |
삼항 연산자 연습 (0) | 2020.09.04 |