简体   繁体   English

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

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

I have a program that prompts a user for integers until they type a value to continue the program onto the next step.我有一个程序,它会提示用户输入整数,直到他们输入一个值以继续执行下一步的程序。 How would I increment a variable called count every time the user inputs an integer?每次用户输入一个整数时,我如何增加一个名为 count 的变量? I'm using count++ to increment the count amount by the way, I just don't know how to make it go up when the user puts in data.顺便说一下,我正在使用count++来增加计数量,我只是不知道如何在用户输入数据时使其增加。

Code so far到目前为止的代码

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

You can put count++;你可以把count++; anywhere in your do loop.在你的 do 循环中的任何地方。

It's simple.这很简单。 Just put count++ inside the while loop as follows,只需将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);

I understand why you're using it but why use the counter at all?我明白你为什么要使用它,但为什么要使用计数器?

The code below uses a different technique to acquire integers from the User.下面的代码使用不同的技术从用户获取整数。 It also ensures that the supplied number is indeed a integer that falls within the Integer.MIN_VALUE and Integer.MAX_VALUE range:它还确保提供的数字确实是一个落在 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);

If you want to keep track of how many entries the User made then you can still apply a counter if you like.如果您想跟踪用户输入了多少条目,那么您仍然可以根据需要应用计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM