java作业之学生类

题目、编写一个Java应用程序。设计一个学生类Students,包括属性有:序号,学号,姓名,性别,专业,三门课程成绩(数学,计算机,英语);包括方法有:求成绩总分,成绩平均分,除序号外各个属性的赋值方法,各个属性值的获取方法等等。说明:每创建一个Students对象,则序号值增1;第一个学生对象的序号值为1

编写一个主类StudentsDemo,在主方法中创建5个Students类的对象,按以下要求输出相关信息:

(1)各课程最高/最低分同学的序号、学号、姓名和该门课程分数

(2)总分最高/最低同学的序号、学号、姓名和所有课程分数

(3)输入一个学号或者姓名,若存在这样的学号或姓名则输出该学生的所有信息,否则输出信息不存在。

package StudentsDemo;
import java.util.Scanner;
public class StudentsDemo {

  public static void main(String[] args) {
    //创建学生信息
    Student[] a = new Student[5];//以数组的形式存储5个学生类
    int i = 0;
    for (i = 0; i < 5; i++) {
      a[i] = new Student();
      a[i].math = (int) (Math.random() * 100);//产生一个0到1之间的随机数*100
      a[i].english = (int) (Math.random() * 100);//随机数录入成绩
      a[i].comp = (int) (Math.random() * 100);
      a[i].gender = "男";
      a[i].major = "计算机类";
//      System.out.println(a[i].nunber);  验证编号
    }
    a[0].stu_num = "125";
    a[1].stu_num = "244";
    a[2].stu_num = "334";
    a[3].stu_num = "442";
    a[4].stu_num = "542";//学号

    a[0].name = "张三";
    a[1].name = "里面";
    a[2].name = "黎明";
    a[3].name = "李华";
    a[4].name = "丽华";//起名

    int max1 = 0, min1 = 0;
    int max2 = 0, min2 = 0;
    int max3 = 0, min3 = 0;
    int MAX = 0;
    for (i = 0; i < 5; i++) {
      if (a[i].gettotal() > a[MAX].gettotal()) {
        MAX = i;
      }
      if (a[i].math > a[max1].math) {
        max1 = i;
      }

      if (a[i].math < a[min1].math) {
        min1 = i;
      }

      if (a[i].english > a[max2].english) {
        max2 = i;
      }

      if (a[i].english < a[max2].english) {
        min2 = i;
      }

      if (a[i].comp > a[max3].comp) {
        max3 = i;//寻找分高的人
      }

      if (a[i].comp < a[min3].comp) {
        min3 = i;
      }
    }
    Put(a, "英语", max2);
    Put(a, "数学", max1);
    Put(a, "计算机", max3);

    //输出学习信息
    Put2(a, "英语", min2);
    Put2(a, "数学", min1);
    Put2(a, "计算机", min3);

    Put(a, "", MAX);
    System.out.println("英语分 " + a[MAX].getEnglish());
    System.out.println("数学分为  " + a[MAX].getMath());
    System.out.println("计算机分数为  " + a[MAX].getComp());
    //输出总分最高

    //查找学生
    int cont = -1;
    Scanner in = new Scanner(System.in);
    System.out.println("请输入学号或姓名");
    String search = in.nextLine();
    for (i = 0; i < 5; i++) {//equals比较字符串是否相等
      if (a[i].name.equals(search) || a[i].stu_num.equals(search)) {
        cont = i;
        break;
      }
    }

    if (cont == -1) {
      System.out.println("信息不存在");
    } else {
      System.out.println("姓名是" + a[cont].name);
      System.out.println("序号是" + a[cont].nunber);
      System.out.println("学号是" + a[cont].stu_num);
      System.out.println("性别是" + a[cont].gender);
      System.out.println("专业是" + a[cont].major);
      System.out.println("英语成绩是" + a[cont].getEnglish());
      System.out.println("数学成绩是" + a[cont].math);
      System.out.println("计算机成绩是" + a[cont].comp);
      System.out.println("总分是" + a[cont].gettotal());
      System.out.println("平均分是" + a[cont].getaver());

    }


  }

  static void Put(Student[] ss, String course, int num) {
    System.out.println(course + "成绩最高的是" + ss[num].nunber + "号学生");
    System.out.println("学号是" + ss[num].stu_num);
    System.out.println("姓名是" + ss[num].name);
    if (course.equals("english")) {//分号不能少
      System.out.println(course + "成绩是" + ss[num].english);
    } else if (course.equals("comp")) {
      System.out.println(course + "成绩是" + ss[num].comp);
    } else if (course.equals("math")) {
      System.out.println(course + "成绩是" + ss[num].math);
    }
  }

  static void Put2(Student[] ss, String course, int num) {
    System.out.println(course + "成绩最低的是" + ss[num].nunber + "号学生");
    System.out.println("学号是" + ss[num].stu_num);
    System.out.println("姓名是" + ss[num].name);
    if (course.equals("english")) {
      System.out.println(course + "成绩是" + ss[num].english);
    } else if (course.equals("comp")) {
      System.out.println(course + "成绩是" + ss[num].comp);
    } else if (course.equals("math")) {
      System.out.println(course + "成绩是" + ss[num].math);
    }
  }
}
package StudentsDemo;
public class Student {

      static int NUM = 1;
      int nunber;
      String stu_num;
      String name;
      String gender;
      double math;
      double comp;
      double english;
      String major;

      Student() {//构造方法
        nunber = NUM++;
      }

      public double gettotal() {//获取总分
        return this.math + this.english + this.comp;
      }

      public double getaver() {
        return ((this.comp + this.english + this.math) * 1.0) / 3;
      }

      public String getName() {
        return name;
      }

      public void setName(String name) {
        this.name = name;
      }

      public double getComp() {
        return comp;
      }

      public void setComp(double comp) {
        this.comp = comp;
      }

      public double getEnglish() {
        return english;
      }

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

      public double getMath() {
        return math;
      }

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

      public String getGender() {
        return gender;
      }

      public void setGender(String gender) {
        this.gender = gender;
      }

      public String getStu_num() {
        return stu_num;
      }

      public void setStu_num(String stu_num) {
        this.stu_num = stu_num;
      }

      public String getMajor() {
        return major;
      }

      public void setMajor(String major) {
        this.major = major;
      }

    }

猜你喜欢

转载自www.cnblogs.com/zhuimingzhenbai/p/12607814.html