次のコードでは、「quit」という単語を入力してコードを停止する必要がありますが、「break」または「system.exit」ステートメントは使用しません。誰かが私を助けることができますか?ブール値でこれを解決できると思いますが、使い方がわかりません。quitステートメントをループの最初の2行に配置しましたが、そこに属しているかどうかはわかりません。私は学習段階にあるので、厳しすぎないでください:))
import java.util.*;
public class Game {
public static void main (String[]args){
Game guessing = new Game();
guessing.start();
}
public void start() {
System.out.println("Welcome to guessing game!");
System.out.println("Please enter the number between 1 and 1000");
Scanner input = new Scanner(System.in);
String playerName;
String currentGuess;
String quit = "quit";
boolean playGame = true;
int tries = 0; //number of times player guessed
int guess = 0; //number that player inputs
long startTime = System.currentTimeMillis(); //start timer after first guess
int randomNumber = (int) (Math.random() * 999 + 1); // generating random number
System.out.println(randomNumber); // to be deleted after game is finished
currentGuess = input.nextLine();
do{
if (currentGuess.equalsIgnoreCase(quit)) {
System.out.println("Thanx for playing");}
if (currentGuess.matches("[0-9]+")) {
int PlayerGuessInt = Integer.parseInt(currentGuess);
}
else {
System.out.println("You have netered non-numeric value,please try again");
currentGuess = input.nextLine();
continue;
}
guess = Integer.parseInt(currentGuess);
if(guess<1 || guess>1000 ){
System.out.println("The number is out of range! Please try again");
currentGuess = input.nextLine();
continue;
}
if (guess>randomNumber){
System.out.println("Oops,too high!");
currentGuess = input.nextLine();
tries++;
}
else if (guess<randomNumber){
System.out.println("Sorry, to low!");
currentGuess = input.nextLine();
tries++;
}
}
while (guess!=randomNumber);
if (guess==randomNumber){
tries++;
long endTime = System.currentTimeMillis();
long gameTime = endTime - startTime;
System.out.println("Well done! You won the game in " + tries + " guesses " + "and " + gameTime/1000 + " seconds!");
System.out.println("Please enter your name: ");
playerName = input.nextLine();
}
}
}