0

for the following code, I need to stop the code by typing the word "quit", but without using "break" or "system.exit" statements. anyone can help me out? I think boolean could solve this but i have no idea how to use it. I placed the quit statement in the first two lines of the loop, but im not sure if it belongs there. Im in my learning phase, so dont be too strict :))

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

        }




}
}
6
  • You don't need that if after the while with an opposite condition since there are no break statements inside the while. It'll always be true.
    – techfoobar
    Oct 3, 2012 at 10:09
  • Make the boolean part of your loop condition and set it to false when you want the loop to stop. Oct 3, 2012 at 10:12
  • @PeterLawrey I`m not sure what i mean, im a bit confused since its my first experience. all i know is that the game must stop when i type quit.
    – G.M
    Oct 3, 2012 at 10:15
  • In this case return; or System.exit(0); will do that. Oct 3, 2012 at 10:17
  • 1
    @user1703849 Then don't use it, use return; instead. Oct 3, 2012 at 10:21

3 Answers 3

1

Change your while loop so that it checks for the value of current guess being "quit". That way it will stop looping when the quit command is given e.g.

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

Put inside the while in one (or all of the if )

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

and change the while to be:

while (guess!=randomNumber && playGame)
0

Here's the answer: Global boolean variable:

bool userWantsToQuit = false;

so if someone types int quit (shouln't it be "quit" because it's string?)

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)
}

at the bottom you change while statement to stop the game if userWantToQuit is true:

(guess!=randomNumber && !userWantsToQuit)

It's been long time since I used java and I didn't test it, but it's definitely in good direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.