ミニアプリ課題:学生の成績管理
では、初心者向けのミニアプリ課題を作ります。
ポイントは以下です:
- ユーザーから 入力 を受け取る
- 多次元配列+ArrayList を活用
- 配列の内容を表示する
課題内容
目的
- ユーザー入力で学生の名前と科目ごとの点数を取得
- 多次元配列を使って各学生の点数を格納
- ArrayListで学生データを管理
- 最後に全員の点数を 見やすく表示
仕様
- ユーザーに学生の人数
nを入力させる - 各学生について:
- 名前を入力
- 科目の数
mを入力 m個の点数を入力
- すべての学生データを ArrayList<int[][]> ではなく
ArrayList<Object[]>のように保持 - 出力例(3名、3科目の場合):
[学生1, [80, 90, 70]]
[学生2, [60, 75, 85]]
[学生3, [90, 95, 100]]
課題ポイント
- ArrayList + 多次元配列の組み合わせ
- ユーザー入力の配列代入
- 配列内容の表示(Arrays.toString / deepToString)
- 例外処理や範囲外アクセスへの注意
模範解答(Java)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class StudentScoresApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Object[]> students = new ArrayList<>();
System.out.print("学生の人数を入力: ");
int n = sc.nextInt();
sc.nextLine(); // 改行を消す
for (int i = 0; i < n; i++) {
System.out.print("学生名: ");
String name = sc.nextLine();
System.out.print("科目数: ");
int m = sc.nextInt();
int[] scores = new int[m];
for (int j = 0; j < m; j++) {
System.out.print((j+1) + "科目の点数: ");
scores[j] = sc.nextInt();
}
sc.nextLine(); // 改行を消す
// 名前と点数配列を Object[] にまとめる
Object[] studentData = {name, scores};
students.add(studentData);
}
System.out.println("\n=== 全学生の点数 ===");
for (Object[] student : students) {
String name = (String) student[0];
int[] scores = (int[]) student[1];
System.out.println("[" + name + ", " + Arrays.toString(scores) + "]");
}
sc.close();
}
}
Java✅ 解説
- ArrayList<Object[]> に学生データを保持
Object[0]→ 名前Object[1]→ 点数配列 (int[])
- 多次元配列の代用として
ArrayList<int[]>でも可能 - Arrays.toString() で点数配列を見やすく表示
- Scanner.nextLine() の扱いに注意 → 数字入力後に改行が残る
出力例
学生の人数を入力: 2
学生名: Taro
科目数: 3
1科目の点数: 80
2科目の点数: 90
3科目の点数: 70
学生名: Hanako
科目数: 2
1科目の点数: 85
2科目の点数: 95
=== 全学生の点数 ===
[Taro, [80, 90, 70]]
[Hanako, [85, 95]]
💡 発展課題アイデア
- 平均点を計算して表示
- 科目名を配列で管理して表示
- データを ArrayList<ArrayList> で保持する
- 入力ミスに対する再入力機能を追加

