Results 1 to 5 of 5
  1. #1
    Malex7 is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Help with Yes/No loop

    I'm new to Programming and know how to do loops, but have not figured out how to do it with String inputs. (Yes/No) Any help?
    Here's what I have
    /*
    Malex
    A program imitating a guessing game from the range 1-100
    */

    import java.util.Scanner;

    public class GuessingGame
    {
    public static void main(String [] args)
    {

    Scanner scan;
    int rnumber,guess; // Comp made random number, user guess, and if you want to play
    String play;
    scan= new Scanner(System.in);

    do
    {
    System.out.print("I'm thinking of a number from 1-100, try and guess... ");

    guess=scan.nextInt(); // This is the user's guess
    rnumber= (int) (Math.random()*100.0)+1; //The random number the computer has thought of
    if (guess>100 || guess<1)
    {
    System.out.println ("ARE YOU DEAF? I SAID 1-100!!! Guess again.");
    guess=scan.nextInt();
    }
    do
    {
    if (guess>100 || guess<1)
    {
    System.out.println ("ARE YOU DEAF? I SAID 1-100!!! Guess again.");
    guess=scan.nextInt();
    }
    if((guess-rnumber)<0)
    {
    System.out.println("Your guess is too low; guess again!");
    guess=scan.nextInt();
    }
    else
    {
    System.out.println("Your guess is too high; guess again!");
    guess=scan.nextInt();
    }
    }
    while ((guess-rnumber)!=0);

    System.out.println("That's right!!!");
    System.out.println("Play again?(Y/N)");
    play=scan.nextLine();
    } while (play==Y||play==y);


    if (play==N ||play==n)
    {
    System.out.print("Let's play later sometime!");
    }
    }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: Help with Yes/No loop

    Never ever compare Strings with ==! Use equals()
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default Re: Help with Yes/No loop

    Alright now its asking if I want to play again, then the program ends.
    Here's the revised ending:
    System.out.println("That's right!!!");
    System.out.println("Play again?(Y/N)");
    play=scan.nextLine();
    } while (play.equals("Y") || play.equals("y"));


    if (play.equals("N") || play.equals("n"))
    {
    System.out.print("Let's play later sometime!");
    }
    }
    }

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: Help with Yes/No loop

    Please use code formatting as described in the announcements to make your code readable. Put in some debug output to see what value play has, when the loop ends. I bet it's different from what you expect.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

Similar Threads

  1. Replies: 7
    Last Post: 04-03-2013, 08:26 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM

Tags for this Thread

Posting Permissions

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