JAVA类与对象 定义一个student类

定义一个student类,其中有成员变量,学号,姓名,性别,是否为班干部,以及语文数学英语的分数,求成绩的方法。
定义一个主类,主方法中通过学生类创造对象,通过键盘输入学生的属性值,然后输出学生的属性值,调用方法输出该学生的总成绩和平均成绩。

import java.util.Scanner;

public class HelpOther {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入学生学号:");
        String id=scanner.nextLine();
        System.out.println("请输入学生姓名:");
        String name=scanner.nextLine();
        System.out.println("请输入学生性别(1:男生 0:女生):");
        String isMan=scanner.nextLine();
        boolean man=false;
        if(isMan.equals("1")){
            man=true;
        }
        System.out.println("请输入学生是否领导(1:是 0:不是)");
        String isLeader=scanner.nextLine();
        boolean leader=false;
        if(isLeader.equals("1")){
            leader=true;
        }
        System.out.println("请输入学生数学分数");
        float mathScore=scanner.nextFloat();
        System.out.println("请输入学生语文分数");
        float chineseScore=scanner.nextFloat();

        Student student=new Student(id,name,man,leader,mathScore,chineseScore);
        System.out.println(student.avScore);

    }

}

class Student {
    String id; //学号
    String name;//姓名
    boolean isMan;//是否男生
    boolean isLeader;//是否领导
    float mathScore;//数学分数
    float chineseScore;//语文分数
    float avScore;//平均成绩

    public Student(String id ,String name,boolean isMan,
                   boolean isLeader,float mathScore,float chineseScore){
        this.id=id;
        this.name=name;
        this.isMan=isMan;
        this.isLeader=isLeader;
        this.mathScore=mathScore;
        this.chineseScore=chineseScore;
        //计算平均分数
        this.avScore=(mathScore+chineseScore)/2;
    }

}


img

如有帮助望采纳哈哈。

怎么这么多student类的问题,我看我是要博客上写个student类的总结报告了。。。

运行结果:

img


//学生类
public class Student {
    //成员变量
    private int studentNumber;
    private String StudentName;
    private String StudentGender;
    private boolean classCadre;
    private double chineseScore;
    private double mathScore;
    private double englishScore;

    //构造方法
    public Student(){
    }

    public int getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(int studentNumber) {
        this.studentNumber = studentNumber;
    }

    public String getStudentName() {
        return StudentName;
    }

    public void setStudentName(String studentName) {
        StudentName = studentName;
    }

    public String getStudentGender() {
        return StudentGender;
    }

    public void setStudentGender(String studentGender) {
        StudentGender = studentGender;
    }

    public boolean isClassCadre() {
        return classCadre;
    }

    public void setClassCadre(boolean classCadre) {
        this.classCadre = classCadre;
    }

    public double getChineseScore() {
        return chineseScore;
    }

    public void setChineseScore(double chineseScore) {
        this.chineseScore = chineseScore;
    }

    public double getMathScore() {
        return mathScore;
    }

    public void setMathScore(double mathScore) {
        this.mathScore = mathScore;
    }

    public double getEnglishScore() {
        return englishScore;
    }

    public void setEnglishScore(double englishScore) {
        this.englishScore = englishScore;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentNumber=" + studentNumber +
                ", StudentName='" + StudentName + '\'' +
                "\n, StudentGender='" + StudentGender + '\'' +
                ", classCadre=" + classCadre +
                "\n, chineseScore=" + chineseScore +
                ", mathScore=" + mathScore +
                ", englishScore=" + englishScore +
                '}';
    }

    //求总成绩的方法
    public double sumScore(){
        return chineseScore+mathScore+englishScore;
    }
}

//测试类
public class StudentDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //创建学生类对象
        Student s = new Student();

        //成员变量赋值
        System.out.print("学号:");
        int number = sc.nextInt();
        s.setStudentNumber(number);

        System.out.print("姓名:");
        String name = sc.next();
        s.setStudentName(name);

        System.out.print("性别:");
        String gender = sc.next();
        s.setStudentGender(gender);

        System.out.print("是否为班干部:");
        while (true){
            String s1 = sc.next();
            if (s1.equals("是")){
                s.setClassCadre(true);
                break;
            }else if (s1.equals("不是")){
                s.setClassCadre(false);
                break;
            }else {
                System.out.println("输入错误,重新输入");
                String s2 = sc.next();
                s1 = s2;
            }
        }

        System.out.print("语文:");
        double num1 = sc.nextDouble();
        s.setChineseScore(num1);

        System.out.print("数学:");
        double num2 = sc.nextDouble();
        s.setMathScore(num2);

        System.out.print("英语:");
        double num3 = sc.nextDouble();
        s.setEnglishScore(num3);

        //输出学生属性值
        System.out.println(s);

        double sumScore = s.sumScore();
        System.out.println("学生总成绩:"+sumScore);
        System.out.println(String.format("学生平均成绩:%.2f",sumScore/3));
    }
}

学生类:

public class Student {

    /**
     * 学号
     */
    private String no;

    /**
     * 姓名
     */
    private String name;

    /**
     * 性别
     */
    private String sex;

    /**
     * 是否为班干部
     */
    private Boolean leader;

    /**
     * 语文分数
     */
    private Double chinese;

    /**
     * 数学分数
     */
    private Double math;

    /**
     * 英语分数
     */
    private Double english;

    /**
     * 总成绩
     *
     * @return
     */
    public Double totalScore() {
        return this.chinese + this.math + this.english;
    }

    /**
     * 平均成绩
     *
     * @return
     */
    public Double avgScore() {
        return this.totalScore() / 3;
    }

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Boolean getLeader() {
        return leader;
    }

    public void setLeader(Boolean leader) {
        this.leader = leader;
    }

    public Double getChinese() {
        return chinese;
    }

    public void setChinese(Double chinese) {
        this.chinese = chinese;
    }

    public Double getMath() {
        return math;
    }

    public void setMath(Double math) {
        this.math = math;
    }

    public Double getEnglish() {
        return english;
    }

    public void setEnglish(Double english) {
        this.english = english;
    }
}

测试代码:

ublic class Main {

    public static void main(String[] args) {
        Student student = new Student();

        Scanner scanner = new Scanner(System.in);

        System.out.println("学号:");
        student.setNo(scanner.nextLine());

        System.out.println("姓名:");
        student.setName(scanner.nextLine());

        System.out.println("性别:");
        student.setSex(scanner.nextLine());

        System.out.println("是否为班干部?Y/N");
        student.setLeader("Y".equalsIgnoreCase(scanner.nextLine()));

        System.out.println("语文分数:");
        student.setChinese(scanner.nextDouble());

        System.out.println("数学成绩:");
        student.setMath(scanner.nextDouble());

        System.out.println("英语成绩:");
        student.setEnglish(scanner.nextDouble());

        System.out.printf("学生信息:学号:%s,姓名:%s,性别:%s,是否为班干部:%s,语文成绩:%s,数学成绩:%s,英语成绩:%s\n",
                student.getNo(), student.getName(), student.getSex(), student.getLeader(), student.getChinese(), student.getMath(), student.getEnglish());
        System.out.println("总成绩:" + student.totalScore());
        System.out.println("平均成绩:" + student.avgScore());
    }
}

img