【发布时间】:2019-12-11 04:52:42
【问题描述】:
我想显示 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");
}
【问题讨论】:
-
if((student1.computerGPA())
-
你能澄清一下“限制输入”是什么意思吗?当您致电
computerGPA时,您目前正在阅读 3 双打,这似乎已经仅限于我。如果我不得不猜测您的问题,我会说 1)一旦读取了 3 个双打,您应该添加一个nextLine(),以便下一次调用nextDouble()从下一行读取和 2)您不应该关闭扫描仪,因为它关闭了底层输入流,这将使对该方法的第二次调用失败