2

ユーザーが数を7回推測しようとした後、do-whileループを停止するのに問題がありました。ループを停止してタイムアウトメッセージを表示するように正確に指示する方法がわかりません。

ありがとう、助けてくれてありがとう!

コード:

import java.util.Random;
import java.util.Scanner;
public class numberGuesses {
     public static void main (String [] args){
            Random rand = new Random();
            int inputNumber;
            int randomNumber = rand.nextInt(100) + 1;
            int guessingLimit = 7;

            System.out.println("Rand Number = " +randomNumber); //Temp to test program with correct number 


            Scanner scan = new Scanner (System.in);

            do {
                System.out.print("Please enter a number: ");
                inputNumber = scan.nextInt();

                if (inputNumber > randomNumber)
                            System.out.println("That's too high!");
                if (inputNumber < randomNumber)
                            System.out.println("That's too low!");
       //       else { 
       //       guessingLimit--;
       //       System.out.println("Too bad! The number was " +randomNumber+ "!"); //Trying to have it print this timeout after 7 incorrect attempts
       //       break;
       //       }
            } while (inputNumber != randomNumber);
                System.out.println("That's right!"); //This line when they guess correctly  
    }

}

正しい出力1:

----exec: java numberGuesses
Rand Number = 67
Please enter a number: 89
That's too high!
Please enter a number: 1
That's too low!
Please enter a number: 67
That's right!

正しい出力2:

----exec: java numberGuesses
Rand Number = 38
Please enter a number: 55
That's too high!
Please enter a number: 66
That's too high!
Please enter a number: 1
That's too low!
Please enter a number: 22
That's too low!
Please enter a number: 44
That's too high!
Please enter a number: 8
That's too low!
Please enter a number: 99
That's too high!
Too bad! The number was 38!
4

2 に答える 2

4

doに、書いて、

  int numAttempts = 0;

以内にdo

  numAttempts++;

状態中:

  while (inputNumber != randomNumber && numAttempts < 7);

完了:

       int numAttempts = 0;
       do {
            System.out.print("Please enter a number: ");
            inputNumber = scan.nextInt();

            if (inputNumber > randomNumber)
                        System.out.println("That's too high!");
            if (inputNumber < randomNumber)
                        System.out.println("That's too low!");
   //       else { 
   //       guessingLimit--;
   //       System.out.println("Too bad! The number was " +randomNumber+ "!"); //Trying to have it print this timeout after 7 incorrect attempts
   //       break;
   //       }
          numAttempts++;
        } while (inputNumber != randomNumber && numAttempts < 7);

        if(inputNumber == randomNumber){
            System.out.println("That's right!"); 
        }else{
            System.out.println("Incorrect 7 attempts"); 
        }
于 2012-10-13T06:54:44.773 に答える
0

状態ユーザーが正しい回数と7回未満の試行回数を入力したかどうかを確認する必要があります。

int inputNumber;
final int randomNumber = rand.nextInt(100) + 1;
final int guessingLimit = 7;
int triesCount = 0;

Scanner scan = new Scanner (System.in);
do {
   inputNumber = scan.nextInt();
   //....
   ++triesCount;
}while(inputNumber != randomNumber && triesCount < guessingLimit);

if (inputNumber == randomNumber) {
   System.out.println("That's right!");
} else {
   System.out.println("Timeout...");
}
于 2012-10-13T07:01:06.110 に答える