3 回答
TA贡献1943条经验 获得超7个赞
public class MaxMinGrades{
public static void main(String[] args){
double maxGrade = 0.0;
double minGrade = 100.0;
Scanner input = new Scanner(System.in);
System.out.println("Enter as many student grades as you like. Enter a character to stop.");
while(input.hasNextDouble()) {
double grades = input.nextDouble();
if (grades > maxGrade) {
maxGrade = grades;
}
if (minGrade > grades) {
minGrade = grades;
}
}
System.out.println("The highest grade is: " + maxGrade);
System.out.println("The lowest grade is: " + minGrade);
}
}
这样, 内部就有了中断条件while loop。当某些事情发生变化时,boolean就会检查条件。
TA贡献1804条经验 获得超3个赞
几点
1. 一旦获得第一个条目,您就需要为 Min 和 Max 分配该值。
2. 变量等级应使用循环内的最新输入进行更新。
public class MaxMinGrades {
public static void main(String[] args){
double maxGrade = Double.MAX_VALUE;
double minGrade = Double.MIN_VALUE;
Scanner input = new Scanner(System.in);
System.out.println("Enter as many student grades as you like. Enter a character to stop.");
double grades = input.nextDouble();
System.out.println("first entry="+grades);
minGrade = maxGrade = grades;
while(input.hasNextDouble()) {
grades = input.nextDouble();
if (grades > maxGrade) {
maxGrade = grades;
}
if (grades < minGrade) {
minGrade = grades;
}
}
System.out.println("The highest grade is: " + maxGrade);
System.out.println("The lowest grade is: " + minGrade);
}
}
TA贡献2051条经验 获得超10个赞
while 循环中没有中断条件。扫描仪将继续侦听输入。仅当输入非双字符时循环才会退出。
您实际上也没有在每次迭代中获得下一个输入。您需要input.nextDouble()在循环内调用。
如果你想让它自己退出,你需要添加一个中断条件。例如,您可以在输入一定数量后停止,例如:
int gradeCount = 0;
while(input.hasNextDouble() && gradeCount < 6) {
grades = input.nextDouble();
gradeCount += 1;
// your logic here
}
添加回答
举报