Java游戏。需要在不使用break语句或system.exit的情况下停止代码

时间:2012-10-03 10:03:55

标签: java

对于以下代码,我需要通过键入单词“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();

        }




}
}

3 个答案:

答案 0 :(得分:1)

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

while(guess!=randomNumber && !currentGuess.equalsIgnoreCase(quit))

答案 1 :(得分:0)

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

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

并将while更改为:

while (guess!=randomNumber && playGame)

答案 2 :(得分:0)

这是答案: 全局布尔变量:

bool userWantsToQuit = false;

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

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为true,则在底部更改while语句以停止游戏:

(guess!=randomNumber && !userWantsToQuit)

自从我使用java以来​​,我已经很久没有测试它,但它肯定是朝着良好的方向发展的。