| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
- iframe 태그
- FileWriter
- FFT
- GridLayout
- 파이썬
- 사전학습
- Codility
- Position
- java
- html 기초
- 반응형 웹 프로젝트
- BorderLayout
- CSS
- rnn
- Database
- css 기초
- 반응형웹
- ObjectOutputStream
- 퍼셉트론
- 푸리에 변환
- 메서드
- HTML
- 미디어쿼리
- html 프로젝트
- 상속
- FlowLayout
- g검정
- oracle
- 예제
- inline
- Today
- Total
도라에몽주머니
[Java/Eclipse] Java 8일차 본문

예제
if-else문을 이용하여 성적처리 프로그램을 작성하시오.
중간고사, 기말고사, 레포트, 출석점수를 입력받아서 계산하시오.
Class name : Prac6.java
조건 1) 아래의 비율이 성적에 반영되도록 할 것
- (중간 + 기말) / 2 → 60%
- 과제 → 20%
- 출석 → 20%
조건 2) 학점은 점수를 기준으로 아래와 같이 구분
90점 이상 : A학점
80점 이상 : B학점
70점 이상 : C학점
60점 이상 : D학점
나머지 : F학점
조건 3) 평가는 학점을 기준으로 아래와 같이 구분
A, B학점 → excellent
C, D학점 → good
F학점 → poor
<실행 결과>
중간고사를 입력하시오 : 90
기말고사를 입력하시오 : 89
과제점수를 입력하시오 : 99
출석점수를 입력하시오 : 100
성적=93.50 (← 소수점 둘째자리까지 출력)
학점=A
평가=excellent
import java.util.Scanner;
public class Prac6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("중간고사를 입력하시오 : ");
int mid = sc.nextInt();
System.out.print("기말고사를 입력하시오 : ");
int fin = sc.nextInt();
System.out.print("과제점수를 입력하시오 : ");
int assignment = sc.nextInt();
System.out.print("출석점수를 입력하시오 : ");
int attendance = sc.nextInt();
System.out.println();
double score = ((double)(mid+fin)/2)*0.6 + assignment*0.2 + attendance*0.2;
System.out.printf("성적=%.2f\n", score);
if(score>=90) {
System.out.println("학점=A");
System.out.println("평가=excellent");
}
else if(score>=80) {
System.out.println("학점=B");
System.out.println("평가=excellent");
}
else if(score>=70) {
System.out.println("학점=C");
System.out.println("평가=good");
}
else if(score>=60) {
System.out.println("학점=D");
System.out.println("평가=good");
}
else {
System.out.println("학점=F");
System.out.println("평가=poor");
}
}
}
switch문
: if문보다 정형화된 조건 판단문
switch(입력변수) {
case 입력값1: ...
break;
case 입력값2: ...
break;
...
default: ...
break;
}
예제
switch문을 이용하여 주민번호 7번째 자리를 입력받은 후,
몇 년대, 성별을 출력하세요. (ex: 123456-0000000)
1 : 1900년대 남성
2 : 1900년대 여성
3 : 2000년대 남성
4 : 2000년대 여성
Class name: Prac7.java
<실행 결과>
주민번호 7번째 자리를 입력하세요 : 1
당신은 1900년대생 남성이십니다.
import java.util.Scanner;
public class Prac7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("주민번호 7번째 자리를 입력하세요 : ");
int id = sc.nextInt();
switch(id) {
case 1: System.out.println("당신은 1990년대생 남성이십니다.");
case 2: System.out.println("당신은 1990년대생 여성이십니다.");
case 3: System.out.println("당신은 2000년대생 남성이십니다.");
case 4: System.out.println("당신은 2000년대생 여성이십니다.");
}
}
}
예제
switch문을 사용하여 성적처리 프로그램을 작성하시오.
중간고사, 기말고사, 레포트, 출석점수를 입력받아서 계산하시오.
Class name: Prac8.java
조건 1) 아래의 비율이 성적에 반영되도록 할 것
- (중간 + 기말) / 2 → 60%
- 과제 → 20%
- 출석 → 20%
조건 2) 학점은 점수를 기준으로 아래와 같이 구분
90점 이상 : A학점
80점 이상 : B학점
70점 이상 : C학점
60점 이상 : D학점
나머지 : F학점
조건 3) 평가는 학점을 기준으로 아래와 같이 구분
A, B학점 → excellent
C, D학점 → good
F학점 → poor
<실행 결과>
중간고사를 입력하시오 : 90
기말고사를 입력하시오 : 89
과제점수를 입력하시오 : 99
출석점수를 입력하시오 : 100
성적=93.50 (← 소수점 둘째자리까지 출력)
학점=A
평가=excellent
import java.util.Scanner;
public class Prac8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String grade="";
System.out.print("중간고사를 입력하시오 : ");
int mid = sc.nextInt();
System.out.print("기말고사를 입력하시오 : ");
int fin = sc.nextInt();
System.out.print("과제점수를 입력하시오 : ");
int assignment = sc.nextInt();
System.out.print("출석점수를 입력하시오 : ");
int attendance = sc.nextInt();
System.out.println();
double score = ((double)(mid+fin)/2)*0.6 + assignment*0.2 + attendance*0.2;
System.out.printf("성적=%.2f\n", score);
switch((int)score/10) {
case 10:
case 9: grade = "A"; break;
case 8: grade = "B"; break;
case 7: grade = "C"; break;
case 6: grade = "D"; break;
default: grade = "F"; break;
}
switch(grade) {
case "A":
case "B": System.out.printf("학점=%s\n평가=exellent", grade); break;
case "C":
case "D": System.out.printf("학점=%s\n평가=good", grade); break;
case "F": System.out.printf("학점=%s\n평가=poor", grade); break;
}
}
}
}'Study > Java' 카테고리의 다른 글
| [Java/Eclipse] Java 10일차 (0) | 2022.09.27 |
|---|---|
| [Java/Eclipse] Java 9일차 (0) | 2022.09.26 |
| [Java/Eclipse] Java 6, 7일차 (1) | 2022.09.22 |
| [Java/Eclipse] Java 5일차 (0) | 2022.09.20 |
| [Java/Eclipse] Java 4일차 (1) | 2022.09.19 |