你好!
public class Course {
String courseName;
int courseHour;
public Course() {
super();
}
public Course(String courseName, int courseHour) {
super();
this.courseName = courseName;
this.courseHour = courseHour;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getCourseHour() {
return courseHour;
}
public void setCourseHour(int courseHour) {
this.courseHour = courseHour;
}
}
public class Student {
String name;
int grade;
Course[] courses;
int hours;
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public Course[] getCourses() {
return courses;
}
public void setCourses(Course[] courses) {
this.courses = courses;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public void showMessage() {
System.out.println(name+" "+grade+" 年级");
System.out.println("选修的课程包括:");
for(int i=0;iSystem.out.println("("+(i+1)+")\t"+courses[i].courseName+"\t"+courses[i].courseHour+" 分");
hours += courses[i].courseHour;
}
System.out.println("总学分:"+hours);
}
}
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.print("输入学生的人数:");
int number = scan.nextInt();
Student[] stus = new Student[number];
for(int i=1;i<=stus.length;i++) {
Student stu = new Student();
System.out.print("请输入第"+i+"个学生的姓名:");
String stuName = scan.next();
stu.setName(stuName);
System.out.print("请输入第"+i+"个学生的年级:");
int grade = scan.nextInt();
stu.setGrade(grade);
System.out.print("请输入第"+i+"个学生选几门课程:");
int courseNum = scan.nextInt();
Course[] _courses = new Course[courseNum];
for(int j=1;j<=_courses.length;j++) {
System.out.print("请输入第"+j+"门课程的名称:");
String courseName = scan.next();
System.out.print("请输入第"+j+"门课程的学分:");
int hour = scan.nextInt();
_courses[j-1] = new Course(courseName, hour);
}
stu.setCourses(_courses);
stus[i-1] = stu;
}
for(int k=1;k<=stus.length;k++) {
System.out.println(""+"第 "+k+" 学生信息如下:");
stus[k-1].showMessage();
}
}
}
希望对你有帮助!
class Student {};
class Course {}