📄 course.java
字号:
package test1;
/*创建一个course类,属性包括(学号、姓名、数学、语文、英语)
1统计总成绩最高的学生信息,然后打印
2统计每科成绩最好的学生信息,并打印。*/
public class Course
{
private int studentId;
private String studentName;
private int mathGrade;
private int chineseGrade;
private int englishGrade;
public int getChineseGrade() {
return chineseGrade;
}
public int getEnglishGrade() {
return englishGrade;
}
public int getMathGrade() {
return mathGrade;
}
public int getStudentId() {
return studentId;
}
public String getStudentName() {
return studentName;
}
public Course(int studentId, String studentName, int mathGrade, int chineseGrade, int englishGrade) {
this.studentId = studentId;
this.studentName = studentName;
this.mathGrade = mathGrade;
this.chineseGrade = chineseGrade;
this.englishGrade = englishGrade;
}
public Course() {}
//统计总成绩最高的学生信息,然后打印
public void totalGradehigh(Course[] cou)
{
int max=0;
for(int i=0;i<cou.length;i++)
{
max=Math.max(max,(cou[i].chineseGrade+cou[i].englishGrade+cou[i].mathGrade));
//Math.min(最小值,元素); Math.max(最大值,数组元素);
}
for(int i=0;i<cou.length;i++)
{
if(cou[i].chineseGrade+cou[i].englishGrade+cou[i].mathGrade==max)
System.out.println(cou[i].getStudentName()+"同学的总成绩最高是"+max+"分"+"\n"+"他的基本信息如下:"+cou[i]);
}
}
// 统计每科成绩最好的学生信息,并打印
public void subjectBest(Course[] cou)
{
int max1=0;
int max2=0;
int max3=0;
//compare()??
for(int i=0;i<cou.length;i++)
{
max1=Math.max(max1,cou[i].chineseGrade);
max2=Math.max(max2,cou[i].mathGrade);
max3=Math.max(max3,cou[i].englishGrade);
}
for(int i=0;i<cou.length;i++)
{
if(cou[i].chineseGrade==max1)
System.out.println("这位同学的语文成绩最好,他的具体信息如下:"+cou[i]);
if(cou[i].mathGrade==max2)
System.out.println("这位同学的数学成绩最好,他的具体信息如下:"+cou[i]);
if(cou[i].englishGrade==max3)
System.out.println("这位同学的英语成绩最好,他的具体信息如下:"+cou[i]);
//else
}
}
public static void main(String[] args)
{
Course[] cou={
new Course(123450, "zhao", 64, 86, 80),
new Course(123451, "qian", 82, 84, 70),
new Course(123452, "sun", 93, 82, 50),
new Course(123453, "li", 53, 71, 90),
new Course(123454, "zhou", 76, 98, 90),
new Course(123455, "wu", 96, 69, 72)
};
Course c=new Course();
c.totalGradehigh(cou);
c.subjectBest(cou);
}
public String toString()
{
return studentId+"\t"+studentName+"\t"+mathGrade+"\t"+chineseGrade+"\t"+englishGrade;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -