每次用户输入数据时如何增加变量?

How to increment a variable every time the user inputs data?

我有一个程序会提示用户输入整数,直到他们键入一个值才能让程序继续下一步。每次用户输入整数时,我如何增加一个名为 count 的变量?顺便说一句,我正在使用count++来增加计数值,我只是不知道如何在用户输入数据时使它上升。

到目前为止的代码

//variables
        int num, count = 0, high, low;
        Scanner userInput = new Scanner(System.in);

        //loop

        do {
                    System.out.print("Enter an integer, or -99 to quit: --> ");
                    num = userInput.nextInt();


                    high = num;
                    low = num;

                    //higher or lower
                    if(count > 0 && num > high)
                    {
                       high = num; 
                    }
                    else if(count > 0 && num < low)
                    {
                        low = num;
                    }
                    else
                    {
                       System.out.println("You did not enter any numbers."); 
                    }

                } while (num != -99);

        System.out.println("Largest integer entered: " + high);
        System.out.println("Smallest integer entered: " + low);

您可以将 count++; 放在 do 循环中的任何位置。

很简单。只需将 count++ 放入 while loop 中,如下所示,

// variables
int num, count = 0, high, low;
Scanner userInput = new Scanner(System.in);

// loop
do {
   System.out.print("Enter an integer, or -99 to quit: --> ");
   num = userInput.nextInt();

   count++; // here it goes

   high = num;
   low = num;

   // higher or lower
   if(count > 0 && num > high)
   {
      high = num; 
   }
   else if(count > 0 && num < low)
   {
      low = num;
   }
   else
   {
       System.out.println("You did not enter any numbers."); 
   }

} while (num != -99);

System.out.println("Largest integer entered: " + high);
System.out.println("Smallest integer entered: " + low);

我明白你为什么要使用它,但为什么要使用计数器?

下面的代码使用不同的技术从用户那里获取整数。它还确保提供的数字确实是落在 Integer.MIN_VALUE 和 Integer.MAX_VALUE 范围内的整数:

Scanner userInput = new Scanner(System.in);
String ls = System.lineSeparator();

int high = 0;
int low = Integer.MAX_VALUE;
int num;
String input = "";
while(input.equals("")) {
    System.out.print("Enter an integer, (q to quit): --> ");
    input = userInput.nextLine().toLowerCase();
    if (input.equals("q")) {
        if (low == Integer.MAX_VALUE) {
            low = 0;
        }
        break;
    }
    if (!input.matches("^-?\d+$")) {
        System.err.println("Invalid Entry (" + input + ")! "
                         + "You must supply an Integer (int) value!" + ls);
        input = "";
        continue;
    }

    boolean invalidInteger = false;
    long tmpVal=0;
    try {
        tmpVal = Long.parseLong(input);
    } catch(NumberFormatException ex) {
        invalidInteger = true;
    }
    if (invalidInteger || tmpVal < Integer.MIN_VALUE || tmpVal > Integer.MAX_VALUE) {
        System.err.println("Invalid Entry (" + input + ")! " + ls
                         + "Number too large (Minimum Allowable: " + Integer.MIN_VALUE 
                         + "  Maximum Allowable: " + Integer.MAX_VALUE + ")!" + ls
                         + "You must supply an Integer (int) value!" + ls);
        input = "";
        continue;
    }
    num = Integer.parseInt(input);
    if (num > high) {
        high = num;
    }
    if (num < low) {
        low = num;
    }
    input = "";
}
System.out.println("Largest integer entered:  " + high);
System.out.println("Smallest integer entered: " + low);

如果您想跟踪用户输入的次数,您仍然可以根据需要应用计数器。