【问题标题】:showing highest and lowest value of a series of objects in java显示java中一系列对象的最高值和最低值
【发布时间】:2021-02-01 18:14:04
【问题描述】:

我必须从给定的用户输入中找到得分最高的学生和得分最低的学生。但我只得到最高分的学生,不能从中得到最低分的学生。


public class Student{

    public String name;
    public String id;
    public int score;
    public static int n;

    public Student(String initName,String initID,int initScore){

        initName=name;
        initID=id;
        initScore=score;
    }

    public Student (){

    }



    public static void main(String[] args) {

        System.out.println("Enter the number of students:");
        Scanner s1=new Scanner(System.in);

      
        Student.n=Integer.parseInt(s1.nextLine().trim());

        System.out.println("Enter the student name,id and score.");
        Scanner s2=new Scanner(System.in);

        Student st1=new Student();
        Student min=new Student(" "," ",100);
        Student max=new Student(" "," ",0);
        for(int i=0;i<Student.n;i++){
            st1.name=s2.next();
            st1.id=s2.next();
            st1.score=s2.nextInt();

            if(max.score<st1.score){
                max.score=st1.score;
                max.name=st1.name;
                max.id=st1.id;
            }
            if(min.score>st1.score){
                min.name=st1.name;
                min.score=st1.score;
                min.id=st1.id;
            }
        }
        System.out.println("the highest scoring student: "+max.name);
        System.out.println("the lowest scoring student: "+min.name);
        
       
    }
}

【问题讨论】:

标签: java


【解决方案1】:
        initScore=score;

请在构造函数中修正以上行。

其实所有的赋值都不对:

    public Student(String initName,String initID,int initScore){

        initName=name;
        initID=id;
        initScore=score;
    }

应该改成

    public Student(String initName, String initId, int initScore) {
        name = initName;
        id = initId;
        score = initScore;
    }

更好

    public Student(String name, String id, int score) {
        this.name = name;
        this.id = id;
        this.score = score;
    }

由于score 在初始构造中没有分配,它被分配了一个默认值0 并因此影响min 计算。 (因为它已经在 min 假设分数是积极的)

它适用于max 计算,因为获得的分数将大于或等于0,并最终将直接更新score(假设只允许正分数)。

【讨论】:

  • tnx 抱歉没有注意到
  • 很高兴有帮助
【解决方案2】:

我发现您的代码中有很多问题,而且您似乎并不真正知道如何编码,这让我想建议您先阅读一些 Java 教程。

现在到你的代码:

  • Student类的实现就是漏洞百出,没有封装,构造函数出错,不知道n是干什么用的

    public class Student {
    
        private String name;
        private String id;
        private int score;
    
        public Student(String initName, String initID, int initScore){
            this.name = initName;
            this.id = initID;
            this.score = initScore;
        }
    
        // declare getters such as these two
        public String getName() {
            return this.name;
        }
    
        public int getScore() {
            return this.score;
        }
    }
    
  • 你的main 方法也有很多错误:你有 2 个Scanners,你没有初始化足够多的学生,你的迭代完全错误

    public static void main(String[] args) {
        Scanner s1 = new Scanner(System.in);
        // get number of students
        System.out.println("Enter the number of students:");
        int numberOfStudents = s1.nextInt();
        // initialize array of students
        Student[] array = new Student[numberOfStudents];
        for(int i = 0; i < numberOfStudents; i++) {
            // get name of the student
            System.out.println("Enter the student name:");
            String name = s1.nextLine();
            // get id of student
            System.out.println("Enter the student id:");
            String id = s1.nextLine();
            // get score of student
            System.out.println("Enter the student score:");
            int score = s1.nextInt();
            array[i] = new Student(name, id, score);
        }
        // now you have students input
        // it's time to find your student
        // set the lowest and highest student to first student
        Student min = array[0];
        Student max = array[0];
        for(int i = 1; i < numberOfStudents; i++) {
            if(min.getScore() > array[i]) {
                min = array[i];
            } else if(max.getScore() < array[i]) {
                max = array[i];
            }
        }
        System.out.println("the highest scoring student: "+max.getName());
        System.out.println("the lowest scoring student: "+min.getName());
    }
    

【讨论】:

    【解决方案3】:

    再次仔细检查代码:

        public Student(String initName,String initID,int initScore){
    
        initName=name;
        initID=id;
        initScore=score;
    }
    

    你收到了吗...?越野车构造函数。 代码应该是

        public Student(String initName,String initID,int initScore){
    
        name = initName;
        id = initID;
        score = initScore;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-28
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多