gpt4 book ai didi

java - 如何用java读取扫描仪中的输入

转载 作者:行者123 更新时间:2023-12-02 02:10:52 25 4
gpt4 key购买 nike

我想显示 GPA 较高的学生的姓名。我想知道如何限制扫描仪中的输入,就像我只想有 2 个学生一样,所以我要计算他们的差距并返回分数最高的那个,请帮忙。

我使用了如下所示的方法扫描仪

public double computerGPA() {

double score;

Scanner sc = new Scanner(System.in);
System.out.println("enter the sutdent quiz score:");
double score1 = sc.nextDouble();
System.out.println("enter the student test score:");
double score2 = sc.nextDouble();
System.out.println("enter the student assigments score");
double score3 = sc.nextDouble();
sc.close();
score = 0.2 * score1 + 0.5 * score2 + 0.3 * score3;

return score;
}
}
if((student1.computerGPA())<(student2.computerGPA())) {
System.out.println("the student "+student2.name+"has higher GPA");
}
if((student2.computerGPA())<(student1.computerGPA())) {
System.out.println("the student "+student1.name+"has higher GPA");
}
if((student1.computerGPA())==(student2.computerGPA())) {
System.out.println("the two students had the same GPA");
}

最佳答案

如果我没看错的话,你会想要在你的学生类(class)中有一个私有(private)双倍,而不是将分数值返回给程序,你可以使用一个 void 方法,将学生的私有(private)双倍设置为该分数

    private double studentScore;

public static void computerGPA() {



Scanner sc= new Scanner(System.in);
System.out.println("enter the sutdent quiz score:");
double score1=sc.nextDouble();
System.out.println("enter the student test score:");
System.out.println("enter the student assigments score");
double score3=sc.nextDouble();
sc.close();

studentScore =0.2*score1+0.5*score2+0.3*score3;

然后你可以为你的两个学生对象提供一个 get 方法并以这种方式进行比较

  if (student1.getGPAScore() > student2.getGPAScore()) {
System.out.println("Student 1 has a higher score");
} else {
System.out.println("Student 2 has a higher score");
}

希望这是有道理的

关于java - 如何用java读取扫描仪中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57328540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com