提问者:小点点

我正在尝试编写一个程序,允许用户以整数形式输入一系列考试成绩


我正在尝试编写一个程序,允许用户输入一系列整数形式的考试分数。

我希望输出看起来像:

输入整数,或输入 -99 退出:80

输入一个整数,或-99退出:95

输入整数,或-99退出:65

输入整数,或-99退出:-99

最大: 95最小: 65

第二轮:

输入整数,或-99退出:-99

您没有输入任何数字。

我把第一部分记下来了,但是当我输入-99时,我似乎不知道如何得到“你没有输入任何数字”行。

这就是我到目前为止所拥有的。

import java.util.Scanner;    // Needed for the Scanner class

/**
   This program shows the largest and smallest exam scores. The user
   enters a series of exam scores, then -99 when finished.
   UPDATED to show even number of points using if-statement
*/

public class Grades
{
   public static void main(String[] args)
   {
      int score = 0;       // Exam score
      int min = 100;       // Hold smallest score
      int max = 0;         // Hold largest score

      // Create a Scanner object for keyboard input.
      Scanner keyboard = new Scanner(System.in);

      // Display general instructions.
      System.out.println("Enter an integer, or -99 to quit: ");
      System.out.println();

      // Get the first exam score.
      System.out.print("Enter an integer, or -99 to quit: ");
      score = keyboard.nextInt();

      // Input exam scores until -99 is entered.
      while (score != -99)
      {
         // Add points to totalPoints.
         if (score > max)
            max = score;
         if (score < min)
            min = score;
         // Get the next number of points.
         System.out.print("Enter an integer, or -99 to quit: ");
         score = keyboard.nextInt();
      }

      // Display the largest and smallest score.
      System.out.println("Largest: " + max);
      System.out.println("Smallest: " + min);
   }
}

共3个答案

匿名用户

引入一个变量来计算输入的数字并在 while 循环中增加呢?

或者,您也可以在while循环后检查数字是否更改:

if(min > max) {
    System.out.println("You did not enter any numbers.");
}
else {
    System.out.println("Largest: " + max);
    System.out.println("Smallest: " + min);
}

这是可行的,因为在开始时,您将< code>min变量初始化为< code>100,将< code>max变量初始化为< code>0,这将导致< code>min的< code>true

匿名用户

输入0时,以下代码不起作用

if (max = 0 && min = 100)
    System.out.println("You did not enter any numbers");
else{
    System.out.println("Largest: " + max);
    System.out.println("Smallest: " + min);
}

使用额外的布尔变量来检查任何与-99不同的输入是一个很好的选择:

public class Grades
{
    public static void main(String[] args)
    {
        int score = 0;       // Exam score
        int min = 100;       // Hold smallest score
        int max = -100;         // Hold largest score
        boolean isAnyScore = false;

        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        // Display general instructions.
        System.out.print("Enter an integer, or -99 to quit: ");
        System.out.println();

        // Input exam scores until -99 is entered.
        while(true)
        {
            System.out.print("Enter an integer, or -99 to quit: ");
            score = keyboard.nextInt(); //Get the exam score.
            if(score == -99) { break; }
            else { isAnyScore = true; }

            // Add points to totalPoints.
            if (score > max) { max = score; }
            if (score < min) { min = score; }
        }

        // Display the largest and smallest score.
        if(!isAnyScore) { System.out.println("You did not enter any numbers"); }
        else
        {
            System.out.println("Largest: " + max);
            System.out.println("Smallest: " + min);
        }
    }
}

变量< code>isAnyScore为假。当您在第一次循环运行中输入-99时,它仍然为假,因为没有赋值。当您在第一次循环运行中输入-99以外的值时,它将始终为真(在任何循环运行中它都将被指定为真)。当< code>isAnyScore从false变为true时,它总是为true,因为您总是指定true,而不是false。

匿名用户

在课程结束时:

if (max == 0 && min == 100)
    System.out.println("You did not enter any numbers");
else{
    System.out.println("Largest: " + max);
    System.out.println("Smallest: " + min);
}