需要从数字列表中显示最高和最低成绩

我需要输入成绩列表,并在输入字符时让代码停止。然后它会显示最高等级和最低等级。


我创建了一个while loop,但是当我运行代码时,它一直循环并且不会停止。


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();


    while(input.hasNextDouble()) {

      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);

  }

}

我会输入:50 66.85 73.5 78.9 77q。


输出将是: 最高等级是:78.9 最低等级是:50.0


绝地无双
浏览 97回答 3
3回答

杨__羊羊

public class MaxMinGrades{&nbsp; public static void main(String[] args){&nbsp; &nbsp; double maxGrade = 0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double minGrade = 100.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter as many student grades as you like. Enter a character to stop.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(input.hasNextDouble()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double grades = input.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (grades > maxGrade) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxGrade = grades;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (minGrade > grades) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minGrade = grades;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The highest grade is: " + maxGrade);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The lowest grade is: " + minGrade);&nbsp; }}这样, 内部就有了中断条件while loop。当某些事情发生变化时,boolean就会检查条件。

狐的传说

几点1. 一旦获得第一个条目,您就需要为 Min 和 Max 分配该值。2. 变量等级应使用循环内的最新输入进行更新。public class MaxMinGrades {public static void main(String[] args){&nbsp; &nbsp; double maxGrade = Double.MAX_VALUE;&nbsp; &nbsp; double minGrade = Double.MIN_VALUE;&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter as many student grades as you like. Enter a character to stop.");&nbsp; &nbsp; double grades = input.nextDouble();&nbsp; &nbsp; System.out.println("first entry="+grades);&nbsp; &nbsp; minGrade = maxGrade = grades;&nbsp; &nbsp; while(input.hasNextDouble()) {&nbsp; &nbsp; &nbsp; &nbsp; grades = input.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; if (grades > maxGrade) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxGrade = grades;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (grades < minGrade) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minGrade = grades;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("The highest grade is: " + maxGrade);&nbsp; &nbsp; System.out.println("The lowest grade is: " + minGrade);}}

侃侃无极

while 循环中没有中断条件。扫描仪将继续侦听输入。仅当输入非双字符时循环才会退出。您实际上也没有在每次迭代中获得下一个输入。您需要input.nextDouble()在循环内调用。如果你想让它自己退出,你需要添加一个中断条件。例如,您可以在输入一定数量后停止,例如:int gradeCount = 0;while(input.hasNextDouble() && gradeCount < 6) {&nbsp; grades = input.nextDouble();&nbsp; gradeCount += 1;&nbsp; // your logic here}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java