Results 1 to 6 of 6
  1. #1
    Luly1224 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Help with y/n question

    Hi, I'm new in Java and I was writing a guessing game code. Everything was fine until I got to the point where I have to add the y/n question. I cant find the right code to do it. Here is my code. Thank you

    package guessinggame;

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

    /**
    *
    * @author Lourdes Rodriguez
    */
    public class GuessingGame {
    private static int guess;



    public static void main(String[] args) {
    Random rand = new Random();
    int numberToGuess = rand.nextInt(100);
    Scanner input = new Scanner(System.in);
    boolean win = false;
    while (win == false){

    System.out.println("Guess a number between 1 and 100:");
    guess = input.nextInt();


    if (guess == numberToGuess){
    win = true;
    }
    else if (guess < numberToGuess){
    System.out.println("Your guess is too low");
    }
    else if (guess > numberToGuess){
    System.out.println("Your guess is too high");
    }
    }

    System.out.println("You win!");
    System.out.println("The number was" + numberToGuess );

    System.out.println("Would you like to play again (Y/N)?/");






    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help with y/n question

    Read the user's response into a String and use a String class's method to compare what was entered.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Luly1224 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help with y/n question

    hi, something like string equal? This goes at the beginning right? I'm new with java and I'm still confuse with the codes. Thank you for rely to me

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help with y/n question

    Use a Scanner class method to read the user's response into a String after the program asks the user the question.

    Yes, you'd use the String class's equals() method to test the user's input.
    Last edited by Norm; 10-27-2013 at 08:29 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Luly1224 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help with y/n question

    Here is my new code but the system show me some errors
    package guessinggame;

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

    /**
    *
    * @author Lourdes Rodriguez
    */
    public class GuessingGame {
    private static int guess;



    public static void main(String[] args) {
    Random rand = new Random();
    int numberToGuess = rand.nextInt(100);
    Scanner input = new Scanner(System.in);
    String choice = in.next();
    boolean win = false;
    while (win == false){

    System.out.println("Guess a number between 1 and 100:");
    guess = input.nextInt();


    if (guess == numberToGuess){
    win = true;
    }
    else if (guess < numberToGuess){
    System.out.println("Your guess is too low");
    }
    else if (guess > numberToGuess){
    System.out.println("Your guess is too high");
    }
    }

    System.out.println("You win!");
    System.out.println("The number was" + numberToGuess );

    System.out.println("Would you like to play again (Y/N)?/");

    if (choice.equalsIgnoreCase("y")){
    System.out.println("guess again");
    }

    else if (choice.equalsIgnoreCase("n")){
    System.out.println("Good bye");
    }



    }

    }

    Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
    at guessinggame.in.next(in.java:16)
    at guessinggame.GuessingGame.main(GuessingGame.java:2 1)
    Java Result: 1

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help with y/n question

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    Java Code:
    String choice = in.next();
    Where is the in variable defined? I don't see it in your code.
    What is that code trying to assign to the choice variable?

    See bold and red in post#4
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 05-25-2012, 04:00 PM
  2. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  3. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 01:14 AM
  4. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  5. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM

Posting Permissions

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