编写一个学生类Student,它具有数据成员:学号【id】、姓名、数学成绩、英语成绩、计算机成绩;具有成员函数:求三门课总成绩的函数sum();求三门课平均成绩的函数average();显示学生数据信息的函数display(),输出每个学生的学号、姓名、数学成绩、英语成绩、计算机成绩 、总成绩、平均成绩。
编写测试类Test,在main方法中输入5个学生信息,或从文件读入5个学生数据,然后输出每个学生的信息,格式如下:
学号 姓名 数学成绩 英语成绩 计算机成绩 总成绩 平均成绩。
XX XX NN NN NN NN NN
思考:如何按下列方式显示
id = 2018115321
name = 张三
cj1 = 87
cj2 = 98
cj3 = 99
总成绩 = 284
平均成绩 = 94
id = 2018131136
name = 蒋平平
cj1 = 67
cj2 = 98
cj3 = 99
总成绩 = 264
平均成绩 = 88
id = 2019131318
name = 谢大脚
cj1 = 87
cj2 = 90
cj3 = 99
总成绩 = 276
平均成绩 = 92
。。。
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(new File("11.txt"));
//System.out.println ("学号\t\t 姓名 数学成绩 英语成绩 计算机成绩 总成绩 平均成绩");
while (sc.hasNextLine()){
String s = sc.nextLine().trim();
String[] aa = s.split("\\s+");
Student st = new Student(aa[0],aa[1],Integer.parseInt(aa[2]),Integer.parseInt(aa[3]),Integer.parseInt(aa[4]));
//st.display();
System.out.println (st.toString());
}
}
catch (Exception ex) {
System.out.println ("error");
}
}
}
class Student{
private String ID,name;
private int cj1,cj2,cj3;
public int sum(){
return cj1+cj2+cj3;
}
public double average(){
return (cj1+cj2+cj3)/3.0;
}
public void display(){
String s = ""+ID +"\t"+ name +"\t"+ cj1 +"\t"+ cj2 +"\t"+ cj3 +"\t"+ sum() +"\t"+ average();
System.out.println (s);
}
public Student(String ID, String name, int cj1, int cj2, int cj3) {
this.ID = ID;
this.name = name;
this.cj1 = cj1;
this.cj2 = cj2;
this.cj3 = cj3;
}
public void setID(String ID) {
this.ID = ID;
}
public void setName(String name) {
this.name = name;
}
public void setCj1(int cj1) {
this.cj1 = cj1;
}
public void setCj2(int cj2) {
this.cj2 = cj2;
}
public void setCj3(int cj3) {
this.cj3 = cj3;
}
public String getID() {
return (this.ID);
}
public String getName() {
return (this.name);
}
public int getCj1() {
return (this.cj1);
}
public int getCj2() {
return (this.cj2);
}
public int getCj3() {
return (this.cj3);
}
public String toString() {
String sep = System.getProperty("line.separator");//\n
StringBuffer buffer = new StringBuffer();
buffer.append(sep);
buffer.append("ID = ");
buffer.append(ID);
buffer.append(sep);
buffer.append("name = ");
buffer.append(name);
buffer.append(sep);
buffer.append("cj1 = ");
buffer.append(cj1);
buffer.append(sep);
buffer.append("cj2 = ");
buffer.append(cj2);
buffer.append(sep);
buffer.append("cj3 = ");
buffer.append(cj3);
buffer.append(sep);
buffer.append("total=");
buffer.append(sum());
buffer.append(sep);
buffer.append("平均=");
buffer.append(average());
return buffer.toString();
}
}