2차원 배열을 사용한 학생정보 시스템
학생 수를 받아와서 배열길이를 조절하고, 각각 성적을 입력해서 평균과 전체 학생 등수를 구하는 프로그램이다.
package march;
import java.util.Scanner;
public class Student_Management_Program_1_2 {
public static void main(String[] args) {
//평균 석차 모두 구해지는 버전
//학생정보관리 프로그램
//학생수에 따라 열이 늘어나는 2차원 배열이다.
int stu_count = 0;
int stu_id = 0;
int kr_s = 0;
int eng_s = 0;
int mat_s = 0;
int rank = 0;
String stu_name = null;
Scanner sc_n = new Scanner(System.in);
Scanner sc_s = new Scanner(System.in);
System.out.println("<Student Management Program>");
System.out.printf("How many Student? : ");
stu_count = sc_n.nextInt();
String[][] stu_info = new String[stu_count][7]; //학생수에 따라 2차원 배열생성
for(int i = 0;i<stu_info.length;i++)
{
for(int k = 0;k<stu_info[i].length;k++)
{
switch(k)
{
case 0 :
System.out.printf("Enter student name : "); //학생이름을 첫번째 배열에 삽입 (i=0부터 시작)
stu_info[i][0] = sc_s.nextLine();
break;
case 1 :
System.out.printf("Enter %s student number : ",stu_info[i][0]); //학생번호을 두번째 배열에 삽입
stu_info[i][1] = sc_s.nextLine();
break;
case 2 :
System.out.printf("Enter %s student korean score : ",stu_info[i][0]); //국어성적을 세번째 배열에 삽입
stu_info[i][2] = sc_s.nextLine();
break;
case 3 :
System.out.printf("Enter %s student english score : ",stu_info[i][0]); //영어성적을 네번째 배열에 삽입
stu_info[i][3] = sc_s.nextLine();
break;
case 4 :
System.out.printf("Enter %s student math score : ",stu_info[i][0]); //수학성적을 다섯번째 배열에 삽입
stu_info[i][4] = sc_s.nextLine();
break;
default:
}
}
}
for(int i = 0;i<stu_info.length;i++) //평균값 구하기을 구해서 6번째 배열에 삽입
{
float avg = ((Float.valueOf(stu_info[i][2]) + Float.valueOf(stu_info[i][3]) + Float.valueOf(stu_info[i][4]))/3);
stu_info[i][5] = String.valueOf(Math.round(avg*100)/100.0);
}
for(int i = 0;i<stu_info.length;i++) //평균값으로 석차 구하기
{
rank = 1;
for(int k=0;k<stu_info.length;k++)
{
if(Float.parseFloat(stu_info[i][5]) < Float.parseFloat(stu_info[k][5]))
{
rank++;
}
}
stu_info[i][6] = Integer.toString(rank);
}
for(int i = 0;i<stu_info.length;i++) //출력
{
System.out.printf("Student name : %s || Student number : %s ||"
+ " Korean : %s || English : %s || Math : %s || "
+ "Average : %s || Rank : %s\n",stu_info[i][0],stu_info[i][1],stu_info[i][2],
stu_info[i][3],stu_info[i][4],stu_info[i][5], stu_info[i][6]);
}
}
}
실행 결과는 다음과 같다.
<Student Management Program>
How many Student? : 3
Enter student name : kim
Enter kim student number : 1
Enter kim student korean score : 100
Enter kim student english score : 95
Enter kim student math score : 92
Enter student name : lee
Enter lee student number : 2
Enter lee student korean score : 100
Enter lee student english score : 100
Enter lee student math score : 90
Enter student name : park
Enter park student number : 3
Enter park student korean score : 40
Enter park student english score : 50
Enter park student math score : 20
Student name : kim || Student number : 1 || Korean : 100 || English : 95 || Math : 92 || Average : 95.67 || Rank : 2
Student name : lee || Student number : 2 || Korean : 100 || English : 100 || Math : 90 || Average : 96.67 || Rank : 1
Student name : park || Student number : 3 || Korean : 40 || English : 50 || Math : 20 || Average : 36.67 || Rank : 3
기능을 추가한다면 입력할때마다 자동으로 학번을 부여하고, 조건문으로 학번으로 정렬하거나 석차로 정령하는 기능을 만들 수 있을 것 같다.