幻晨7 2022-03-18 08:45 采纳率: 100%
浏览 67
已结题

怎么在键盘上输入被private定义变量的值

问题遇到的现象和发生背景

学生的成绩只能在一开始定义,不知道怎么修改成从电脑上输入,成绩只能是固定的值
(刚学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

我的解答思路和尝试过的方法
我想要达到的结果

学生的成绩可以从电脑上输入,而不是一直固定不变的

  • 写回答

4条回答 默认 最新

  • CSDN专家-sinJack 2022-03-18 08:55
    关注
    
    public class test {
     public static void main(String[] args) {
      Scanner in=new Scanner(System.in);
      Student student1=new Student("001","小明");
      student1.setEnglish(in.nextDouble());
      student1.setMaths(in.nextDouble());
      student1.setComputer(in.nextDouble());
      System.out.println(student1+"\t测试成绩是\t"+student1.testScore());
      Student student2=new StudentXW("002","小李");
      student2.setEnglish(in.nextDouble());
      student2.setMaths(in.nextDouble());
      student2.setComputer(in.nextDouble());
      System.out.println(student2+"\t测试成绩是\t"+student2.testScore());
      Student student3=new StudentBZ("003"," 小王");
      student3.setEnglish(in.nextDouble());
      student3.setMaths(in.nextDouble());
      student3.setComputer(in.nextDouble());
      System.out.println(student3+"\t测试成绩是\t"+student3.testScore());
     }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月26日
  • 已采纳回答 3月18日
  • 创建了问题 3月18日

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染