# Thread: Help with Yes/No loop

1. Member
Join Date
Oct 2013
Posts
2
Rep Power
0

## 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. ## Re: Help with Yes/No loop

Never ever compare Strings with ==! Use equals()

3. Member
Join Date
Oct 2013
Posts
2
Rep Power
0

## 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. ## 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.

5. ## Re: Help with Yes/No loop

Program Skipping Input