Java定义学生类,成员变量包括姓名、性别、年龄、语文成绩、数学成绩和英语成绩,求总分和平均分。

定义一个学生类Student,成员变量包括姓名、性别、年龄、语文成绩、数学成绩和英语成绩,求总分和平均分。

控制台输入成绩,根据菜单栏选择显示:

public class Student {
    private String name;
    private String sex;
    private int age;
    private double chineseScore;
    private double mathScore;
    private double englishScore;

    public Student() {
    }

    public Student(String name, String sex, int age, double chineseScore, double mathScore, double englishScore) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
    }

    public double getTotalScore() {
        return englishScore + mathScore + chineseScore;
    }

    public double getAvgScore() {
        return getTotalScore() / 3.0;
    }

    @Override
    public String toString() {
        return "学生信息: {" +
                "姓名: '" + name + '\'' +
                ", 性别: '" + sex + '\'' +
                ", 年龄: " + age +
                ", 语文: " + chineseScore +
                ", 数学: " + mathScore +
                ", 英语: " + englishScore +
                '}';
    }
}
public class Grade {
    private Student student;


    public Grade(Student student) {
        this.student = student;
    }

    public void menu() {
        System.out.println("菜单栏:");
        System.out.println("1. 显示学生信息,");
        System.out.println("2. 求成绩总分,");
        System.out.println("3. 求成绩平均分");
        System.out.println("0. 退出");
    }

    public void show(int type) {
        switch (type) {
            case 1:
                System.out.println(this.student.toString());
                break;
            case 2:
                System.out.println(this.student.getTotalScore());
                break;
            case 3:
                System.out.println(this.student.getAvgScore());
                break;
            default:
                System.out.println("输入错误!");
        }
    }
}
public class TestMain {
    public static void main(String[] args) {
        System.out.println("输入学生信息:");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入学生姓名:");
        String name = scanner.next();
        System.out.println("输入学生性别:");
        String sex = scanner.next();
        System.out.println("输入学生年龄:");
        int age = scanner.nextInt();
        System.out.println("输入学生语文成绩:");
        double chineseScore = scanner.nextDouble();
        System.out.println("输入学生数学成绩:");
        double mathScore = scanner.nextDouble();
        System.out.println("输入学生英语:");
        double englishScore = scanner.nextDouble();

        Student student = new Student(name, sex, age, chineseScore, mathScore, englishScore);

        Grade grade = new Grade(student);
        grade.menu();
        while (true){
            int type = scanner.nextInt();
            if (type == 0) {
                break;
            }
            grade.show(type);

        }
    }
}

运行结果:

img

class Student{
    private String name;
    private String sex;
    private int age;
    private int englishScore;
    private int mathScore;
    private int chnScore;
    public int getTotalScore(){
        return englishScore + mathScore + chnScore;
    }
    public float getAvgScore() {
        return getTotalScore()/3.0;
   }
   Student() {}
   Student(String n,String s,int a,int e,int m,int c) {
        name = n;
        sex = s;
        age = a;
        englishScore = e;
        mathScore = m;
        chnScore = c;
   }
}


public class Student {
    public String name;
    public String sex;
    public int age;
    public double en;
    public double zh;
    public double math;
    public Student() {
    }
    public Student(String name, String sex, int age, double en, double zh, double math) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.en = en;
        this.zh = zh;
        this.math = math;
    }
    //求平均分
    public double avg(double en,double zh,double math){
        return (en+zh+math)/3;
    }
    public static void main(String[] args) {
        Student s=new Student();
        double avg = s.avg(89, 88, 97);
        System.out.println(avg);
    }
}

测试结果如图

img

怎么改为运行时输入数据和求总和呢