7. IF 제어문 + 퀴즈
by mini_min◾ if 제어문은 실행문이 하나라면 {} 를 생략 가능하다.
Scanner sc = new Scanner(System.in);
int a, b;
System.out.print("정수는 ? ");
a = sc.nextInt();
b = a;
if(a < 0) { // 조건이 만족할 때 실행하는 실행문이 하나면 {} 생략 가능
b = -a;
}
◾ if 문으로 평년/윤년 구분하기
if 문 하나와 else if , else 를 사용할 수 있다.
Scanner sc = new Scanner(System.in);
int y;
String s;
System.out.print("년도 ? ");
y = sc.nextInt();
if(y%4==0 && y%100!=0 || y%400==0) {
s = "윤년";
} else {
s = "평년";
}
System.out.println(y+"년도는 "+s+"입니다.");
sc.close();
◾ 작은수부터 큰 수 순서로 출력하기
출력을 위해 temp (비어 있는 변수) 가 필요하다.
Scanner sc = new Scanner(System.in);
int a, b, c, temp;
System.out.println("세 수 입력 ? ");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
// a값이 가장 큰 값
if(a>b) {
temp=a; a=b; b=temp;
}
if(a>c) {
temp=a; a=c; c=temp;
}
if(b>c) {
temp=b; b=c; c=temp;
}
System.out.println(a + "," + b + "," + c);
◾ 대문자, 소문자, 기타 문자 구분을 위해 비교 연산자를 사용한다.
Scanner sc = new Scanner(System.in);
// 한 문자를 입력 받아 대문자, 소문자, 숫자문자, 기타문자인지 판별
char c;
String s;
System.out.print("한 문자는? ");
c = sc.next().charAt(0);
if(c>='A' && c<='Z') {
s = "대문자";
} else if(c>= 'a' && c<='z') {
s = "소문자";
} else if(c>='0' && c<='9') {
s = "문자숫자";
} else {
s = "기타문자";
}
System.out.println(c + " -> " + s);
◾ 배수 확인하기, % 연산자를 이용해서 나머지 값을 가지고 판단한다.
Scanner sc = new Scanner(System.in);
int n;
System.out.print("정수는 ? ");
n = sc.nextInt();
if(n%3==0 && n%4==0) { // 3과 4의 배수인지 먼저 판별해야하기 때문에 순서 주의하기!
System.out.println(n + "은 3과 4의 배수");
}
else if(n%3==0) {
System.out.println(n + ": 3의 배수");
}
else if(n%4==0) {
System.out.println(n + ": 4의 배수");
}
else {
System.out.println("3과 4의 배수가 아님");
}
◾ 수를 입력 받아서 차이 구하기
Scanner sc = new Scanner(System.in);
int a, b, c;
System.out.print("첫번째수? ");
a = sc.nextInt();
System.out.print("두번째수? ");
b = sc.nextInt();
if(a>b) {
c = a - b;
}
else {
c = b - a;
}
System.out.print("두수의 차이 : " + c);
◾ 급여 구하기
Scanner sc = new Scanner(System.in);
int h, pay;
System.out.print("근무 시간은 ? ");
h = sc.nextInt();
pay = h * 10000;
if(h > 8) {
pay = 8 * 10000 + (int)((h-8) * 10000 * 1.5);
}
else {
pay = h * 10000;
}
System.out.printf("급여 : %,d", pay);
◾ 연산자 부호까지 입력받아서 계산하기
if 문, else if 문을 여러 번 써서 계산한다.
Scanner sc = new Scanner(System.in);
int a, b, t=0;
char y;
System.out.print("두수는 ? ");
a = sc.nextInt();
b = sc.nextInt();
System.out.print("연산자[+, -, *, /] ? ");
y = sc.next().charAt(0);
if(y == '+') {
t = a + b;
} else if(y == '-') {
t = a - b;
} else if(y == '*') {
t = a * b;
} else if(y == '/') {
t = a / b;
}
else {
System.out.println("연산자 입력 오류");
}
System.out.printf("%d %c %d = %d", a, y, b, t);
◾ 점수 입력 받아 합격/불합격 판단하기
Scanner sc = new Scanner(System.in);
String name;
int a, b, c;
int ave;
System.out.print("이름 ? ");
name = sc.next();
System.out.print("세 과목 점수? ");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
ave = (a + b + c)/3;
if (ave >= 60 && a>=40 && b>=40 && c >=40) {
System.out.println(name + "님은 합격 입니다.");
}
else if(a < 40 || b < 40 || c < 40) {
System.out.println(name + "님은 과락 입니다.");
}
else {
System.out.println(name + "님은 불합격 입니다.");
}
◾ 점수, 레포트, 결석 등 정보 입력 받아 평점 구하기
Scanner sc = new Scanner(System.in);
int mid, fin, x, report;
String num, name;
int x1=0, total;
char a;
System.out.print("학번 ? ");
num = sc.next();
System.out.print("이름 ? ");
name = sc.next();
System.out.print("중간고사 점수 ? ");
mid = sc.nextInt();
System.out.print("기말고사 점수 ? ");
fin = sc.nextInt();
System.out.print("결석 횟수 ? ");
x = sc.nextInt();
if(x <= 1) x1 = 100;
else if(x <= 3) x1 = 80;
else if(x <= 5) x1 = 60;
else x1 = 0;
System.out.print("레포트 점수 ? ");
report = sc.nextInt();
total = (int)(mid * 0.4 + fin * 0.4 + x1 * 0.1 + report * 0.1);
// 이렇게 전체 연산하면 안된다. 결과값이 다르게 나옴
// 위에 처럼 썼더니 원하는 답은 안나옴
// 따로 따로 0.4 , 0.1 곱해줘야함
// 소수점이 1위로 올라갈 수 있기 때문에
// 하나씩 연산하는 것이 맞다.
/*
* a*=0.4;
* b*=0.4;
* c*=0.1;
* 이런식으로 계산해야함.
*/
if(total >= 90) a = 'A';
else if(total >= 80) a = 'B';
else if(total >= 70) a = 'C';
else if(total >= 60) a = 'D';
else a = 'F';
System.out.println("학번\t 이름\t 중간고사\t 기말고사\t 출석점수\t 레포트\t 합산점수\t 학점\t");
System.out.printf("%s\t %s\t %d\t %d\t %d\t %d\t %d\t %c\t", num, name, mid, fin, x1, report, total, a);
'개발 공부중 > 📑 코드 복습' 카테고리의 다른 글
9. for 문 (0) | 2023.02.22 |
---|---|
8. switch case 문 (1) | 2023.02.22 |
6. 연산자 - 퀴즈 (0) | 2023.02.22 |
5. 연산자 (0) | 2023.02.22 |
4. 확장 문자열 : 키보드로 표현할 수 없는 문자들 (0) | 2023.02.22 |
블로그의 정보
개발자 미니민의 개발로그
mini_min