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

时间:2016-10-17 11:01:20

标签: java

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

我希望输出看起来像:

输入一个整数,或输入-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);
   }
}

6 个答案:

答案 0 :(得分:1)

如何引入一个计算输入数字的变量并在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);
}

这样可行,因为在开始时,您将min变量初始化为100,将max变量初始化为0,这将导致true min > max检查是否输入了数字。

答案 1 :(得分:1)

以下代码在您输入0&amp;时不起作用100:

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

变量isAnyScore为false。当你在第一次循环运行中输入-99时,它仍然是false,因为没有赋值。当你在第一次循环运行中输入-99以外的东西时,它总是为真(它将在任何循环中作为true运行)。当isAnyScore从false变为true时,它始终是真的,因为你总是分配true,永远不会伪造。

答案 2 :(得分: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);
}

答案 3 :(得分:0)

public static void main(String[] args) {
    int score = 0;       // Exam score
    int min = 100;       // Hold smallest score
    int max = 0;         // Hold largest score
    boolean isNumberEntered = false; //Test if any number has been entered.

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

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

        if (score == -99) {
            break;
        } else {
            // If the first number is entered
            if (!isNumberEntered)
                isNumberEntered = true;
            // Add points to totalPoints.
            if (score > max)
                max = score;
            if (score < min)
                min = score;
        }


    }

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

}

如果你试试这个怎么样?你想留在循环中,直到添加了一个数字或者是你想要它的方式吗?

答案 4 :(得分:0)

我认为等级在100到0之间。所以我改变了一点。如果输入是&lt; 0和&gt; 100,输出为&#34;您没有输入任何数字&#34;。

  package chapter5;


import java.util.Scanner; 
public class Grades
{
   public static void main(String[] args)
   {

      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.print("Enter an integer, or -99 to quit: ");
     int score = keyboard.nextInt();
      if(score<0||score>100){
          System.out.println("You did not enter any numbers.");
      }

      // Input exam scores until -99 is entered.
      if(score>=0||score<=100){
      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();
          if(score==-99){
                System.out.println();}

                else if(score!=-99&&score<0||score>100){
          System.out.println("You did not enter any numbers.");}

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


   }
}

答案 5 :(得分:0)

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
        Scanner keyboard = new Scanner(System.in);
        // Display general instructions.
        int i = 1;
        do {
            System.out.println("Enter an integer, or -99 to quit: ");
            score = keyboard.nextInt();
            if (score > max) {
                max = score;
            }
            if (score < min) {
                min = score;
            }
            i++;
            if (i > 5) {
                break;
            }
        } while ((score != -99));

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