第2个回答 2013-09-20
可以专门的定义一个学生类class Student{ ArrayList a = new ArrayList ; private int num;//学号
private String name;//姓名
private int[] score = new int[5];//5课成绩
private int avg;//平均分 Student(){}// 通过输入学生的学号和姓名,再输入学生的成绩
Student(int num, String name) throws IOException {
this.num = num;
this.name = name; int sum = 0; System.out.println("请输入该同学的5科成绩");
for (int i = 0; i < score.length; i++) {
score[i] = sc.nextInt();
sum += score[i];
}
avg = (int) sum / score.length; } public String toString()//为了输出学生信息{ public String toString() {
String information = num + " " + name + " " + score[0] + " "
+ score[1] + " " + score[2] + " " + score[3]+ " " + score[4];
return information; } // 单个学生的输入信息
public void input() throws IOException {
System.out.println("请输入学生的信息");
System.out.println(" 学号" + " " + " 姓名");
int num = sc.nextInt();
String name = sc.next();
a.add(new Student(num, name));// 放置每个学生
} public static vodi main(String[] args){ Student a = new Student(); for(int i=0;i<5;i++) { a.input(); } }}} 这个代码是我以前做过的一的小项目中的截取片段,思路是,将学生的信息,单独存放在Student类中,在类中增加一个 加入学生信息的方法,在主方法中去调用就可以了,这样想输几个就输几个