import java.util.Scanner;
//배열예제 : 학생수 입력받고, 학생의 이름과 나이를 입력받아 출력하기
public class Quiz_05_extra {
//나이를 입력할 학생의 수를 입력하세요 : 2
//1번째 학생의 이름 : Tom //영어로 입력받을 것 //한글은 오류날 가능성이 있어서
//Tom 학생의 나이 : 15
//2번째 학생의 이름 : susan
//Susan 학생의 나이 : 20
//========결과=========
//Tom 학생은 15세입니다.
//susan 학생은 20세입니다.
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
int count;
String[] student_name;
int[] student_age;
int[] student_korean;
int[] student_english;
int[] student_sum;
double[] student_avg;
//배열 참조 변수를 선언해놓고, 배열은 아래에서 만들어도 된다.
//배열참조변수를 만들때 배열도 꼭 같이 만들어야하는 필수사항은 아니다.
while(true) {
try {
System.out.print("입력할 학생의 수를 입력하세요 : ");
count=Integer.parseInt(sc.nextLine()); // 학생의 수 입력받기
//try-catch 예외처리문에서는 예외를 처리하고자하는 부분만 넣어주는게 좋다.(아까는 배열 생성부분까지 넣었었는데 지금은 큰 차이 없지만 나중에는 큰 차이가 있을 수도 있음???????)
}catch(Exception e) {
System.out.println("숫자를 입력해주세요.");
continue;
}
break;
}
//index에 count를 넣어 학생의 수만큼 String배열과 int배열이 각각 생성됨
student_name = new String[count];
student_age=new int[count];
student_korean=new int[count];
student_english=new int[count];
student_sum=new int[count];
student_avg=new double[count];
//학생의 수만큼 반복문이 돌면서 이름과 나이 입력 받기(28~40 Line)
for(int i=0;i<count;i++) {
System.out.print((i+1)+"번째 학생의 이름 : ");
student_name[i]=sc.nextLine();
System.out.print(student_name[i]+ "학생의 나이 : ");
student_age[i]=Integer.parseInt(sc.nextLine());
System.out.print(student_name[i]+ "학생의 국어 : ");
student_korean[i]=Integer.parseInt(sc.nextLine());
System.out.print(student_name[i]+ "학생의 영어 : ");
student_english[i]=Integer.parseInt(sc.nextLine());
student_sum[i]=student_korean[i]+student_english[i];
student_avg[i]=student_sum[i]/2;
}
/*
*
*
//
System.out.println("===========결과===========");
for(int i=0;i<count;i++) {
System.out.println(student_name[i]+" 학생은 " + student_age[i] + "세입니다.");
}
*/
//출력하기 (50~57 Line)
System.out.println("===================결과===================");
System.out.println("이름\t나이\t국어\t영어\t합계\t평균");
System.out.println("------------------------------------------");
for(int i=0;i<count;i++) {
System.out.print(student_name[i] + "\t"+ student_age[i] + "\t"+ student_korean[i] + "\t"+ student_english[i] + "\t"+ student_sum[i] + "\t"+ student_avg[i]);
System.out.println();
}
}
}
Comment