求解,不知道自己哪里数组越界
【问题描述】P84 题目2
【输入形式】键盘输入学生人数,再依次输入这些学生的相关信息,包括姓名、年级、选课数,以及这几门所选课程的名称、学分信息。具体格式见样例。
【输出形式】依次输出所有学生的相关信息,包括学生姓名、年级,此学生所选选课的名称、学分,以及总学分数。具体格式见样例。
【样例输入】
input student count:2
input no 1 student name:Lucy
input no 1 student grade:2
input no 1 student course count:2
input no 1 course name:C
input no 1 course hour:2
input no 2 course name:DS
input no 2 course hour:3
input no 2 student name:Hellen
input no 2 student grade:3
input no 2 student course count:3
input no 1 course name:Java
input no 1 course hour:3
input no 2 course name:linux
input no 2 course hour:2
input no 3 course name:html
input no 3 course hour:2
【样例输出】
no 1 student information:
Lucy 2 grade
courses:
(1)C:2 hour
(2)DS:3 hour
hours:5
no 2 student information:
Hellen 3 grade
courses:
(1)Java:3 hour
(2)linux:2 hour
(3)html:2 hour
hours:7
【样例说明】
【评分标准】要求编写学生类Student、课程类Course和客户端类Client。学生类Student、课程类Course中应有合理的构造方法和数据成员的访问接口,编写学生类Student的getHours()方法,计算学生选课的总学分;编写学生类Student的ShowMessage()方法,打印输出学生信息(姓名、年龄、所有选课的名称、学分及总学分);编写测试类,在控制台输入课程和学生信息,并打印出这些学生的选课信息。
代码如下:
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static class Student {
String name;
String grade;
Course[] num;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public Course[] getNum() {
return num;
}
public void setNum(Course[] num) {
this.num = num;
}
public Student(String name, String grade, Course[] num) {
super();
this.name = name;
this.grade = grade;
this.num = num;
}
@Override
public String toString() {
return name +grade+ " grade "+"\n"+"courses:"+"\n"+Arrays.toString(num);
}
public static class Course {
private String name;
private int hour;
public Course(String name, int hour) {
super();
this.name = name;
this.hour = hour;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
@Override
public String toString() {
return "Course [name=" + name + ", hour=" + hour + "]";
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("input student count:");
int a=sc.nextInt();
Student[] array=new Student[a];int i=1; int e=0;
for(Student arr:array) {
System.out.println("input no "+i+" student name:");
String name=sc.next();
System.out.println("input no "+(i)+" student grade:");
String grade=sc.next();
System.out.println("input no "+(i)+" student course count:");
int cnum=sc.nextInt();
Course[] brr=new Course[cnum];
for(int j=1;j<=cnum;j++){
System.out.println("input no "+(j)+" course name:");
String cname=sc.next();
System.out.println("input no "+(j)+" course hour:");
int hour =sc.nextInt();
brr[j]=new Course(cname,hour);
}arr=new Student(name,grade,brr);
System.out.println("no "+i+" student information:"+"/n"+name +grade+ " grade "+"\n"+"courses:"+"\n");
for(int k=1;k<=cnum;k++)
{System.out.println("("+(k)+")"+brr[k].name+":"+brr[k].hour+" hour"+"\n");
e+=brr[k].hour;}
System.out.print("hours:"+e);
i++; }
}}
}