ユーザーが数を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!