保存到桌面加入收藏设为首页
问题哥
当前位置:首页 > 资源问答

关于Java的Array.sort的一个小问题

2021-04-21 22:51:40   问题哥   来源网络   375   0
import java.time.LocalDate;
import java.util.Arrays;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
class Module{ 
    private String stuName;
    private double credit;
    private double grade;
 
    public Module(String stuName, double grade, double credit){ 
        this.stuName = stuName;
        this.grade = grade;
        this.credit = credit;
    }
 
    String getName(){ 
        return stuName;
    }
 
    double getCredit(){ 
        return credit;
    }
     
    double getGrade(){ 
        return grade;
    }
}
 
class Student { 
    //Fields
    private long stuID;
    private String stuName;
    private char stuGender;
    private LocalDate stuBDay;
    private String stuMajor;
    private Module[] modules;
     
    //Constructor
    public Student(long stuID, String stuName,char stuGender, int year, int month, int day, String stuMajor){
        this.stuID = stuID;
        this.stuName = stuName;
        this.stuGender = stuGender;
        this.stuBDay = LocalDate.of(year, month, day);
        this.stuMajor = stuMajor;
        modules = new Module[5];
    }
 
    // //Methods
    public long getID(){ 
        return stuID;
    }
    public String getName(){ 
        return stuName;
    }
    public char getGender(){ 
        return stuGender;
    }
    public int getAge(){ 
        return 2021-stuBDay.getYear();
    }
    public String getMajor(){ 
        return stuMajor;
    }
    double getGPA(){ 
        double sumCredits = 0;
        double sumGrades = 0;
        for(int i=0; i<5; i++){ 
            sumGrades += modules[i].getGrade()*modules[i].getCredit();
            sumCredits += modules[i].getCredit();
        }
        return sumGrades/sumCredits;
    }
    double getLowestP(){ 
        double lowest = Double.POSITIVE_INFINITY;
        for(int i = 0; i < modules.length; i++)
            if(lowest > modules[i].getGrade())
                lowest = modules[i].getGrade();
        return lowest;
    }
    double getHighestP(){ 
        double highest = Double.NEGATIVE_INFINITY;
        for(int i = 0; i < modules.length; i++)
            if(highest < modules[i].getGrade())
                highest = modules[i].getGrade();
        return highest;
    }
    void setModule(Module x){ 
        for(int i = 0; i < modules.length; i++){
            if(modules[i] == null){
                modules[i] = x;
                // System.out.println(modules[i].getGrade());
                break;
            }
        }
    }  
     
}
 
public class HW7_20203293012 {
    public static void main(String[] args){ 
         
        //Han Mei
        Student hanmei = new Student(10010, "Han Mei", 'm', 2002, 8, 23, "Math");
        hanmei.setModule(new Module("Han Mei", 98, 1.5));
        hanmei.setModule(new Module("Han Mei", 78, 0.5));
        hanmei.setModule(new Module("Han Mei", 68, 1.0));
        hanmei.setModule(new Module("Han Mei", 95, 2.0));
        hanmei.setModule(new Module("Han Mei", 82, 3.0));
 
        //Xiao Ming
        Student xiaoming = new Student(10010, "Xiao Ming", 'm', 2003, 1, 2, "Math");
        xiaoming.setModule(new Module("Xiao Ming", 100, 1.5));
        xiaoming.setModule(new Module("Xiao Ming", 65, 0.5));
        xiaoming.setModule(new Module("Xiao Ming", 68, 1.0));
        xiaoming.setModule(new Module("Xiao Ming", 75, 2.0));
        xiaoming.setModule(new Module("Xiao Ming", 61, 3.0));
 
        //Lao Liu
        Student laoliu = new Student(10010, "Lao Liu", 'm', 2002, 5, 10, "Math");
        laoliu.setModule(new Module("Lao Liu", 98, 1.5));
        laoliu.setModule(new Module("Lao Liu", 99, 0.5));
        laoliu.setModule(new Module("Lao Liu", 93, 1.0));
        laoliu.setModule(new Module("Lao Liu", 89, 2.0));
        laoliu.setModule(new Module("Lao Liu", 96, 3.0));
 
     
        System.out.println(hanmei.getGPA());
        System.out.println(xiaoming.getName());
        System.out.println(laoliu.getName());   
        Student[] stus = new Student[3];
        stus[0] = hanmei;
        stus[1] = xiaoming;
        stus[2] = laoliu; 
         
        //Get the student with the highest GPA
        Student bestStu = stus[0];
        for(int i=0; i<stus.length; i++){
            if(stus[i].getGPA() > bestStu.getGPA())
            bestStu = stus[i];
        }
         
        System.out.println(laoliu.getAge());
 
        System.out.println(laoliu.getHighestP());
 
        // System.out.print(bestStu.getName());
 
        // Array.sort(stus, (s1, s2) -> s1.getHighestP() - s2.getHighestP());  //为什么这个不能用
 
        // Arrays.sort(stus, (Student s1,Student s2) ->{ 
        //     if(s1.getHighestP() - s2.getHighestP() > 0)
        //     return 1;
        // else
        //     return 0; 
        // });          //为什么这个可以用但是不能排序
        System.out.print(stus[0].getName());
        System.out.print(stus[1].getName());
        System.out.print(stus[2].getName());
    }
}
为什么这里面的Array.sort一直都connot be resolved ?(我注释的那一部分)

我已经试过一万种方法了。 各种论坛都问了。还是没有结果,只能来问问吾爱的大佬大佬们了



1.本站所有文章内容均来自网络收集或网友投稿;2.文章内如有软件或者附件如有需要请尽快备份下载失效不补,本站不提保存备份;3.作者投稿可能会经我们编辑修改或补充。

标签:问题  一个  小问题  
相关评论

本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解! 

侵权删帖/违法举报/投稿等事物联系邮箱:adminad@email.cn

51La