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

Kaiju :

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? 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.

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);
Hasitha Jayawardana :

It's simple. Just put count++ inside the while loop as follows,

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=370653&siteId=1