JAVA 배열 (int, String, float) 예제

*** 검색어: nabiro, JAVA, 배열

[출처] (주)프리렉, 열혈강의 JAVA Programming, 김승현 저


import java.io.*;

public class P219 {
	public static void main(String[] ar) throws IOException {
		BufferedReader in = new BufferedReader(
				new InputStreamReader(System.in));
		String[] jumsuname = {"국어", "영어", "수학"};
		int[] jumsu = new int[jumsuname.length + 1]; 	
			// 0:국어, 1:영어, 2:수학, 3:종합
		float avg = 0.0f;
		
		for(int i=0; i < jumsu.length - 1; i++){
			do {
				System.out.print(jumsuname[i] + " = ");
				jumsu[i] = Integer.parseInt(in.readLine());
			}while(jumsu[i] < 0 || jumsu[i] > 100);
			jumsu[jumsu.length - 1] += jumsu[i];
		}
		
		avg = jumsu[jumsu.length - 1] / (float)(jumsu.length - 1);
		
		System.out.println();
		System.out.println("총점 = " + jumsu[jumsu.length -1]);
		System.out.println("평균 = " + avg);
	}
}


댓글

Designed by JB FACTORY