Results 1 to 4 of 4
  1. #1
    MrM
    MrM is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default guessing game problem

    i have to Modify the program to remove the "sudden stop" of the two calls to System.exit(0). id like to use the break technique, but I want the code to leave the while loop as soon as the user wins. I do not want the program to output "You lose" when the user wins.


    my code//

    import java.util.Scanner;
    import java.util.Random;

    public class Guess {

    static int lowNumber=1;
    static int highNumber=100;
    static int numOfGuessesAllowed=10;

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Random generator = new Random();
    int target = generator.nextInt(highNumber) + lowNumber;
    System.out.println(target);
    int numOfGuesses=0;
    while(numOfGuesses < numOfGuessesAllowed) {

    System.out.println("Guess a number:");
    int guess = sc.nextInt();
    boolean hasWon = checkNumber(guess, target);
    numOfGuesses++;
    if(hasWon) {
    System.out.println("You win");
    System.exit(0);
    }
    }
    System.out.println("You lose");
    System.exit(0);
    }

    public static boolean checkNumber(int guess, int target) {
    if(guess < target) {
    System.out.println("Higher");
    } else if (guess > target) {
    System.out.println("Lower");
    }
    return guess==target;

    }



    public static int getNumberFromUser(Scanner sc) {

    }

    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    While loops can have as many conditions as you like. So maybe you could move the declaration of the hasWon boolean. Then you could use it in the while loop. You could also use it in an if statement to determine if your display "You lose" or not.

    By the way, why did you start another thread to ask the same question?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Ah!

    I see the other one got closed.

  4. #4
    MrM
    MrM is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. guessing game
    By MrM in forum New To Java
    Replies: 1
    Last Post: 02-02-2011, 07:51 PM
  2. Need help in Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 10:43 PM
  3. Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 08:00 PM
  4. guessing game help
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 10-31-2009, 05:37 PM
  5. guessing game using GUI
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 10-26-2009, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •