Results 1 to 13 of 13
Thread: How to make this work?
- 02-15-2013, 06:46 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
[Solved] Looping for correct input
It took me a while to realize how to do it, but thanks to the help of you guys, I got it working with this code. :) (I omitted the rest of my program)
Java Code:import java.util.Scanner; class ThisThingIMade { public static void main(String[] args) { System.out.println("Would you like to play? (Yes/No)"); Scanner scanner = new Scanner(System.in); String yesNo = "Derp"; do { yesNo = scanner.nextLine(); if(yesNo.equalsIgnoreCase("yes")) { System.out.println("What is 7 x 6?"); } else if(yesNo.equalsIgnoreCase("no")) { System.out.println("Giving up already? Panzy..."); System.out.println("Press enter to exit."); try { System.in.read(); } catch(Exception e) {} return; } else { System.out.println("Please answer yes or no, this program isn't a genius."); yesNo = "Derp"; } } while(!yesNo.equalsIgnoreCase("yes") && !yesNo.equalsIgnoreCase("no"));Last edited by Aceflare0; 02-16-2013 at 06:02 AM.
- 02-15-2013, 07:11 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: How to make this work?
You need a loop. My suggestion would be to use the do-while loop.
Read the tutorial and try it again :-)
- 02-15-2013, 08:01 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
Re: How to make this work?
Edit: I used do-while and it loops like I want it now, but when you put in yes after you've put an incompatible answer it goes to the next block without sending the next question.
Console:
Java Code:C:\Documents and Settings\Owner\Desktop\Will's game\Random stuff>java ThisThingI Made Would you like to play? (Yes/No) huh Please answer yes or no, this program isn't a genius. yes 42 Woah, ye beat meh.
Java Code:import java.util.Scanner; class ThisThingIMade { public static void main(String[] args) { System.out.println("Would you like to play? (Yes/No)"); Scanner scanner = new Scanner(System.in); String yesNo = scanner.next(); do { if(yesNo.equalsIgnoreCase("yes")) { System.out.println("What is 7 x 6?"); } else if(yesNo.equalsIgnoreCase("no")) { System.out.println("Giving up already? Panzy..."); } else { System.out.println("Please answer yes or no, this program isn't a genius."); yesNo = scanner.next(); } } while(!yesNo.equalsIgnoreCase("yes") && !yesNo.equalsIgnoreCase("no")); double fortyTwo = scanner.nextDouble(); if(fortyTwo == 42) { System.out.println("Woah, ye beat meh."); } else { System.out.println("YOU LOSE!"); } } }Last edited by Aceflare0; 02-15-2013 at 08:41 AM.
- 02-15-2013, 10:04 AM #4
Re: How to make this work?
Please go through the Forum Rules, particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-15-2013, 10:40 AM #5
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: How to make this work?
Hi Aceflare0, welcome to the forums.
The reason you are not seeing the next question is the way the do while loop works. The loop validates the condition after executing the code within it, unlike a do loop which validates the condition before executing the code.
At the end of the loop you ask the user to put their input in again. The do while loop then uses this input to validate the condition.
Post back if you require assistance correcting this issue.
Regards.
- 02-15-2013, 02:55 PM #6
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
Re: How to make this work?
Yeah I'm just completely lost now >_<; if the program knows the condition has just then been filled, why does it not then execute the condition's code? How do I make it do it?
- 02-15-2013, 03:07 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: How to make this work?
OK, this is the flow it goes through when you input an incorrect value followed by a correct value.Java Code:String yesNo = scanner.next(); do { ... handle 'yes' or 'no' ... } else { System.out.println("Please answer yes or no, this program isn't a genius."); yesNo = scanner.next(); } } while(!yesNo.equalsIgnoreCase("yes") && !yesNo.equalsIgnoreCase("no")); double fortyTwo = scanner.nextDouble(); if (fortyTwo == 42) { System.out.println("Woah, ye beat meh."); } else { System.out.println("YOU LOSE!"); }
It hits the 'while' with a correct value, so exits, which you don't want.
Since this is doWhile, put the initial read of yes/no inside the do-while, and remove the read at the end.
That will mean it will read only inside the loop and will not read again at the end, which is what's causing your problem.Please do not ask for code as refusal often offends.
- 02-15-2013, 03:29 PM #8
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 59
- Rep Power
- 0
- 02-15-2013, 03:37 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: How to make this work?
Try/catch is utterly pointless here.
The OP has almost the correct code, they just need to move where they read the input.Please do not ask for code as refusal often offends.
- 02-15-2013, 03:41 PM #10
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 59
- Rep Power
- 0
- 02-15-2013, 03:42 PM #11
Godlike
- Join Date
- Nov 2012
- Posts
- 194
- Rep Power
- 1
Re: How to make this work?
Original Poster
- 02-15-2013, 04:17 PM #12
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 59
- Rep Power
- 0
- 02-16-2013, 06:05 AM #13
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
New to java stop need help stop
By Skrap09 in forum New To JavaReplies: 3Last Post: 09-17-2011, 12:45 PM -
Use stop button to stop moving (stop timers) on JPanel
By mneskovic in forum New To JavaReplies: 3Last Post: 07-23-2010, 12:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
whats that?

Bookmarks