JAVA/수업 복습
자바 공부기록20 - 배열변수 활용하여 성적표 출력하기
본이qq
2022. 4. 15. 16:59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package days14;
import java.util.Scanner;
public class Array04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.printf("학생이 몇명입니까?");
int std = Integer.parseInt(sc.nextLine());
int [] kor = new int[std]; // 국어점수들 저장용 배열
int [] eng = new int[std]; // 영어점수들 저장용 배열
int [] mat = new int[std]; // 수학점수들 저장용 배열
int [] tot = new int[std]; // 총점 용
double [] avg = new double [std]; //평균용
String [] name = new String[std]; //이름용
|
cs |
- 학생이 몇명이냐는 질문에 nextInt로 하지 않고 nextLint으로 답하는 이유
nextInt에는 Enter 입력버퍼가 남아있다
따라서 입력 버퍼에 남아있던 'Enter'는 바로 다음에 나오는 sc.nextLine()에 자동으로 입력되어 문자입력의 기회를
상실할 수 있어서, 'Enter'가 모두 입력되는 sc.nextLine();로 먼저 입력 받고 숫자로 변경
- String -> int 변경 함수
int std = Integer.parseInt(sc.nextLine());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
for (int i = 0; i <std ; i++) {
System.out.printf("%d번 학생의 이름 : ",i+1 );
name[i] = sc.nextLine();
System.out.printf("%d번 학생의 국어 점수 : ",i+1 );
kor[i] = Integer.parseInt( sc.nextLine());
System.out.printf("%d번 학생의 영어 점수 : ", i+1 );
eng[i] = Integer.parseInt( sc.nextLine());
System.out.printf("%d번 학생의 수학 점수 : ",i+1 );
mat[i] = Integer.parseInt( sc.nextLine());
}
for(int i = 0; i<std; i++) {
tot[i] =kor[i] + eng[i] + mat[i];
avg[i] = tot[i] / 3.0;
}
|
cs |
-반복문 for를 이용하여 a[0]번부터 a[std-1]까지 이름, 성적, 총점, 평균을 대입
1
2
|
for( int i = 0; i<std; i++) //std 대신에 kor.length로 써도 됨 (크기가 똑같기 때문)
System.out.printf("%d\t%s\t\t%d\t%d\t%d\t%d\t%.2f\n", (i+1),name[i],kor[i],eng[i],mat[i],tot[i],avg[i]);
|
cs |
-출력하기