SerenityU 2023-03-30 20:10 采纳率: 50%
浏览 15
已结题

代码最后方法重写错了

创建和显示学生信息,具体要求如下
定义Person类,在该类中定义成员变量name(姓名)sex (性别)和 age (年龄),然后定义有参构造方法,初始成员变量,最后定义成员方法oString0返回姓名、性别和年龄信息。
定义 Student 类继承erson类,在该类中增加成员变量no(学号)、scoreEn(英语成绩)、scoreMath (数学成绩)和 screCh (语文成绩),然后增加成员方法aver、max和min分别返回三门功课的平均分、最育分和最低分,最后重写 toString方法返回姓名、性别、年龄、学号、平均分、最高分和最低信息。
定义 StudentTest 类在 main0方法中实现创建和显示学生信息

  • 写回答

1条回答 默认 最新

  • cyixian 2023-03-30 20:15
    关注
    
    class Person {
        String name;
        String sex;
        int age;
    
        public Person(String name, String sex, int age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
    
        public String toString() {
            return "姓名:" + name + ",性别:" + sex + ",年龄:" + age;
        }
    }
    
    class Student extends Person {
        String no;
        int scoreEn;
        int scoreMath;
        int scoreCh;
    
        public Student(String name, String sex, int age, String no, int scoreEn, int scoreMath, int scoreCh) {
            super(name, sex, age);
            this.no = no;
            this.scoreEn = scoreEn;
            this.scoreMath = scoreMath;
            this.scoreCh = scoreCh;
        }
    
        public double aver() {
            return (scoreEn + scoreMath + scoreCh) / 3.0;
        }
    
        public int max() {
            int max = scoreEn > scoreMath ? scoreEn : scoreMath;
            max = max > scoreCh ? max : scoreCh;
            return max;
        }
    
        public int min() {
            int min = scoreEn < scoreMath ? scoreEn : scoreMath;
            min = min < scoreCh ? min : scoreCh;
            return min;
        }
    
        public String toString() {
            return super.toString() + ",学号:" + no + ",平均分:" + aver() + ",最高分:" + max() + ",最低分:" + min();
        }
    }
    
    public class StudentTest {
        public static void main(String[] args) {
            Student stu = new Student("张三", "男", 18, "20210001", 80, 90, 70);
            System.out.println(stu.toString());
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月31日
  • 已采纳回答 3月31日
  • 创建了问题 3月30日

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了