问题遇到的现象和发生背景
学生的成绩只能在一开始定义,不知道怎么修改成从电脑上输入,成绩只能是固定的值
(刚学java,可以的话麻烦解释得详细点)
问题相关代码,请勿粘贴截图
package 父类与子类声明;
class Student implements Comparable{
public Student(String no, String name) {
this.no = no;
this.name = name;
}
public Student() {
}
public Student(String no, String name, double english, double maths,
double computer, double sum) {
this.no = no;
this.name = name;
this.english = english;
this.maths = maths;
this.computer = computer;
this.sum = sum;
}
private String no;
private String name;
private double english;
private double maths;
private double computer;
private Double sum=0.0;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
sum+=english;
}
public double getMaths() {
return maths;
}
public void setMaths(double maths) {
this.maths = maths;
sum+=maths;
}
public double getComputer() {
return computer;
}
public void setComputer(double computer) {
this.computer = computer;
sum+=computer;
}
public Double getSum() {
return sum;
}
public void setSum(Double sum) {
this.sum = sum;
}
public String toString() {
return "学号:"+no+"\t姓名\t"+name+"\t英语\t"+english+"\t数学\t"+maths+"\t计算机\t"+computer+"\t总成绩\t"+sum;
}
public int compareTo(Object o) {
return sum.compareTo(((Student)o).getSum());
}
public Double sum(){
return english+maths+computer;
}
public Double testScore(){
return sum/3;
}
}
class StudentXW extends Student{
public StudentXW(String no, String name) {
super(no,name);
}
public StudentXW() {
super();
}
private String duty;
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
public Double testScore() {
return super.testScore()+3;
}
}
class StudentBZ extends Student{
public StudentBZ(String no, String name) {
super(no,name);
}
public StudentBZ() {
super();
}
private String duty;
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
public Double testScore() {
return super.testScore()+5;
}
}
public class test {
public static void main(String[] args) {
Student student1=new Student("001","小明");
student1.setEnglish(60.0);
student1.setMaths(60.0);
student1.setComputer(60.0);
System.out.println(student1+"\t测试成绩是\t"+student1.testScore());
Student student2=new StudentXW("002","小李");
student2.setEnglish(70.0);
student2.setMaths(70.0);
student2.setComputer(70.0);
System.out.println(student2+"\t测试成绩是\t"+student2.testScore());
Student student3=new StudentBZ("003"," 小王");
student3.setEnglish(80.0);
student3.setMaths(80.0);
student3.setComputer(80.0);
System.out.println(student3+"\t测试成绩是\t"+student3.testScore());
}
}
运行结果及报错内容
学号:001 姓名 小明 英语 60.0 数学 60.0 计算机 60.0 总成绩 180.0 测试成绩是 60.0
学号:002 姓名 小李 英语 70.0 数学 70.0 计算机 70.0 总成绩 210.0 测试成绩是 73.0
学号:003 姓名 小王 英语 80.0 数学 80.0 计算机 80.0 总成绩 240.0 测试成绩是 85.0
我的解答思路和尝试过的方法
我想要达到的结果
学生的成绩可以从电脑上输入,而不是一直固定不变的