0

对于以下代码,我需要通过键入单词“quit”来停止代码,但不使用“break”或“system.exit”语句。任何人都可以帮助我吗?我认为布尔值可以解决这个问题,但我不知道如何使用它。我将退出语句放在循环的前两行,但我不确定它是否属于那里。我在我的学习阶段,所以不要太严格:))

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

        }




}
}
4

3 回答 3

1

更改您的 while 循环,以便它检查当前猜测的值是否“退出”。这样,当给出退出命令时,它将停止循环,例如

while(guess!=randomNumber && !currentGuess.equalsIgnoreCase(quit))
于 2012-10-03T10:13:53.170 回答
0

将 while 放入一个(或所有 if )中

if (currentGuess.equals("quit") {
   playGame = false
}

并将时间更改为:

while (guess!=randomNumber && playGame)
于 2012-10-03T10:15:22.620 回答
0

答案是:全局布尔变量:

bool userWantsToQuit = false;

所以如果有人输入 int quit (不应该是“退出”,因为它是字符串吗?)

if (currentGuess.equalsIgnoreCase(quit)) 
{
    userWantsToQuit = true; // we mark that someone wants to quit
    System.out.println("Thanx for playing"); // we output message
    continue; // skip execution of the rest (or you could use else if)
}

如果 userWantToQuit 为真,则在底部更改 while 语句以停止游戏:

(guess!=randomNumber && !userWantsToQuit)

很久没用java了,也没有测试过,但肯定是朝着好的方向发展。

于 2012-10-03T10:29:43.700 回答