Results 1 to 5 of 5
Thread: Help with Yes/No loop
- 10-07-2013, 06:07 AM #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!");
}
}
}
- 10-07-2013, 10:18 AM #2
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
- 10-07-2013, 04:34 PM #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!");
}
}
}
- 10-07-2013, 04:40 PM #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.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 10-08-2013, 02:04 AM #5
Re: Help with Yes/No loop
http://www.java-forums.org/new-java/...ing-input.html
Read the above, especially reply 16.
Similar Threads
-
When you have a choice between using either a for loop or a while loop...
By psx2514 in forum New To JavaReplies: 7Last Post: 04-03-2013, 08:26 PM -
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 02:25 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM
Bookmarks