たびに、ユーザーの入力データを変数をインクリメントするには?

怪獣:

私は、彼らが次のステップにプログラムを継続するために値を入力するまでの整数をユーザに促しプログラムを持っています。どのように私は変数は、ユーザが整数を入力するたびにカウントと呼ばれるインクリメントでしょうか?私は単なるデータではときにユーザープットそれが上がるようにする方法がわからない、途中でカウント量をインクリメントする++のカウントを使用しています。

コードのこれまで

//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:

簡単だ。ジャストは入れ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);

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=371023&siteId=1