// 課題) // 1、5人分の、番号、国語の点数、算数の点数を格納できるような、2次元配列を作成する // 2、次にキーボードから5人の番号と国語と算数の点数を入力して配列に格納する。 // 3、次に国語と算数の合計が高いものを順に、番号と国語と算数をならびかえる。 // 4、最後に合計の高い順に番号を画面に出力する // 番号 国語 算数 合計 // 1 90 80 170 // 2 70 70 140 // 3 95 70 165 // 4 40 50 90 // 5 30 0 30 //のような配列を考える。 // // import java.io.*; class Sample13 { public static void main(String[] args) throws IOException { int student; int[][] students = new int[5][4]; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < students.length; i++) { for (int j = 0; j < students[i].length - 1; j++) { switch (j) { case 0: System.out.print("番号 = "); break; case 1: System.out.print("国語 = "); break; case 2: System.out.print("算数 = "); break; } try { String inputLine = br.readLine(); students[i][j] = Integer.parseInt(inputLine); } catch (NumberFormatException e) { System.out.println("入力データが不正です"); System.exit(1); } catch (IOException e) { System.out.println("入力時にエラーがありました"); System.exit(1); } } students[i][3] = students[i][1] + students[i][2]; } for (int i = 0; i < students.length - 1; i++) { for (int j = i + 1; j < students.length; j++) { if (students[i][3] < students[j][3]) { for (int k = 0; k < students[j].length; k++) { student = students[i][k]; students[i][k] = students[j][k]; students[j][k] = student; } } } } System.out.println("\n番号\t合計\t国語\t算数"); for (int i = 0; i < students.length; i++) { System.out.printf("%4d\t%4d\t%4d\t%4d%n", students[i][0], students[i][3], students[i][1], students[i][2]); } } }