发表于: 2017-09-06 22:56:05
1 924
今天完成的事情: 假期在补JAVA基础,卡在任务17,任务中出现的词汇好多概念都不懂,没有继续做任务。
学习了简单类中多对象对应多对象的引用传递方法,抄写程序“学生选课及成绩查询”。
明天计划的事情:明天继续学习JAVA基础static关键字的使用。
遇到的问题:用idea时,直接javac *.java(包含中文)会报错。settings和默认settings中都已经改成utf-8。
必需用javac -encoding UTF-8 *.java代码才能编译,如何解决?
收获:
把代码放上来
class Student {
//基本属性
private int stuid ;
private String name ;
private int age ;
//引用属性
private Course courses [];
private StudentCourse [] studentCourses;
//构造方法
public Student () {}
public Student (int stuid,String name,int age){
this.stuid = stuid;
this.name = name;
this.age = age;
}
//普通方法
public void setStudentCourses(StudentCourse[] studentCourses) {
this.studentCourses = studentCourses;
}
public StudentCourse[] getStudentCourses() {
return studentCourses;
}
/*StudentCourse中包含了课程与学生的关系
public void setCourses(Course [] courses){
this.courses = courses;
}
public Course[] getCourses() {
return this.courses;
}
*/
public String getInfo(){
return "name = " + this.name + ",id = " + this.stuid + ",age = " + this.age;
}}
class Course {
private int cid ;
private String name ;
private int credit ;
private Student [] students;
private StudentCourse [] studentCourses;
public Course(){}
public Course(int cid,String name,int credit){
this.cid = cid;
this.name = name;
this.credit = credit;
}
public void setStudentCourses(StudentCourse[] studentCourses) {
this.studentCourses = studentCourses;
}
public StudentCourse[] getStudentCourses() {
return studentCourses;
}
/* StudentCourse中包含了课程与学生的关系
public void setStudents(Student [] students){
this.students = students;
}
public Student[] getStudents() {
return this.students;
}
*/
public String getInfo(){
return "课程编号:" + this.cid + ", 名称:" + this.name + ",学分" + this.credit;
}}
class StudentCourse {//学生选课信息及成绩
private Student student ;
private Course course ;
private double score ;
public StudentCourse(){}
public StudentCourse(Student student,Course course,double score){
this.course = course;
this.score = score;
this.student = student;
}
public Student getStudent() {
return student;
}
public Course getCourse() {
return course;
}
public double getScore() {
return score;
}}
public class CourseDemo {
public static void main (String args[]){
Student stu1 = new Student(1,"zhang",18);
Student stu2 = new Student(2,"li",19);
Student stu3 = new Student(3,"wu",20);
Course ca = new Course(101,"mazhe",3);
Course cb = new Course(102,"shuxue",4);
//设置学生的课程
stu1.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu1,ca,99.7),
new StudentCourse(stu1,cb,89.2)
});
stu2.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu2,ca,98.2),
new StudentCourse(stu2,cb,79.2)
});
stu3.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu3,ca,97.2)
});
//设置课程信息
ca.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu1,ca,99.7),
new StudentCourse(stu2,ca,98.2),
new StudentCourse(stu3,ca,97.2)
});
cb.setStudentCourses(new StudentCourse[]{
new StudentCourse(stu1,cb,89.2),
new StudentCourse(stu2,cb,79.2)
});
//找的一门课程中所有学生的信息成绩
System.out.println("课程信息\n" + ca.getInfo());
for (int x = 0 ; x < ca.getStudentCourses().length ; x++){
System.out.print(ca.getStudentCourses()[x].getStudent().getInfo());
System.out.println(", score = " + ca.getStudentCourses()[x].getScore());
}
//找到一个学生的所有课程信息和成绩
System.out.println("===================================================");
System.out.println(stu2.getInfo());
for (int x = 0 ; x < stu2.getStudentCourses().length ; x ++){
System.out.print(stu2.getStudentCourses()[x].getCourse().getInfo());
System.out.println(stu2.getStudentCourses()[x].getScore());
}
}
}
输出结果
评论