Results 1 to 14 of 14
Thread: help me plss
- 12-08-2008, 06:47 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
help me plss
How to return to the start if i get the answer wrong
Java Code:import java.util.Scanner; public class guessgame { public static void main( String args[] ) {Scanner input=new Scanner(System.in); double a=1185188;// pls crack the answer urself double b=8211;//me cant tell wat the number mean double c=19151514;//this secret sorry double d; double e; double f; System.out.print("Please guess who I love\nThe answer should given in code nomber"); System.out.print("\nPlease type your answer"); System.out.print("\nFirst name:"); d=input.nextDouble(); System.out.print("\nMiddle name:"); e=input.nextDouble(); System.out.print("\nEnd name:"); f=input.nextDouble(); if(a+b+c==d+e+f) System.out.print("You are correct!!!!"); if(a+b+c!=d+e+f) System.out.print("You are incorrect!!!!!\nPlease try again!!!!"); }}//end class
-
usually a while loop works nicely here. you can have a boolean variable that you set to true if answered, false if not, and then put all the user interaction stuff in the while loop and only exit the loop if the answer is true. (but how are they going to guess numbers like that?). Also, you may wish to look into tacking an else block after your if block instead of another if (xxx != yyy).
e.g.
Java Code:boolean done = false; while (!done) // while not done { // prompt for user input // get user input // test user input // if user input good then done = true }Last edited by Fubarable; 12-08-2008 at 06:57 PM.
- 12-08-2008, 06:57 PM #3
Maybe put it into a while loop, and use the continue command if you get it wrong and break if you get it right.
The one who laughs last is teh stupid.
:3
- 12-08-2008, 07:05 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
-
I'm not the best teacher in the world, but fortunately I know of a great tutorial for this, the Sun Java tutorials. Have a look here:
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Best of luck.
-
- 12-08-2008, 07:17 PM #7
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
Finally thx 4 ur help.The code work
- 12-08-2008, 07:39 PM #8
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
sorry,yes the program start from beginning but when i wrong second time the program will end.i need a program return again and again when i wrong again and again.Java Code:import java.util.Scanner; public class guessgame { public static void main( String args[] ) {Scanner input=new Scanner(System.in); double a=1185188;//answer double b=8211;// double c=19151514;// double d; double e; double f; System.out.print("Please guess who I love\nThe answer should given in code nomber"); System.out.print("\nPlease type your answer"); System.out.print("\nFirst name:"); d=input.nextDouble(); System.out.print("\nMiddle name:"); e=input.nextDouble(); System.out.print("\nEnd name:"); f=input.nextDouble(); if(a+b+c==d+e+f) System.out.print("You are correct!!!!"); if(a+b+c!=d+e+f) System.out.print("You are incorrect!!!!!\nPlease try again!!!!"); boolean done = false; while (!done) // while not done { System.out.print("\nFirst name:"); d=input.nextDouble(); System.out.print("\nMiddle name:"); e=input.nextDouble(); System.out.print("\nEnd name:"); f=input.nextDouble(); done=true; } }}//end classLast edited by low224; 12-09-2008 at 06:36 AM.
- 12-08-2008, 08:37 PM #9
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
What you could probably use is switch case with terinary operators.
- 12-08-2008, 08:54 PM #10
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
:eek:
Well the previous problem i had solve with the code above.Java Code:import java.util.Scanner; public class guessgame { public static void main( String args[] ) {Scanner input=new Scanner(System.in); double a=1185188;//answer double b=8211;// double c=19151514;// double d; double e; double f; System.out.print("Please guess who I love\nThe answer should given in code nomber"); System.out.print("\nPlease type your answer"); System.out.print("\nFirst name:"); d=input.nextDouble(); System.out.print("\nMiddle name:"); e=input.nextDouble(); System.out.print("\nEnd name:"); f=input.nextDouble(); if(a+b+c==d+e+f) System.out.print("You are correct!!!!"); if(a+b+c!=d+e+f) System.out.print("You are incorrect!!!!!\nPlease try again!!!!"); boolean done = false; while (!done) // while not done { System.out.print("\nFirst name:"); d=input.nextDouble(); System.out.print("\nMiddle name:"); e=input.nextDouble(); System.out.print("\nEnd name:"); f=input.nextDouble(); if(a+b+c==d+e+f) System.out.print("You are correct!!!!"); if(a+b+c!=d+e+f) System.out.print("You are incorrect!!!!!\nPlease try again!!!!"); } }}
But there is a new problem...
How do I end the loop when i get the correct answer?
Pls tell me...Last edited by low224; 12-09-2008 at 06:38 AM.
- 12-08-2008, 09:00 PM #11
once it's correct just set done to true
- 12-09-2008, 05:32 AM #12
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
the done=true is put inside the loop statement or outside the loop statement???
-
One way to solve this: figure out on paper your program flow, actually walk through your code as if the program were running and see where it naturally fits.
Then put it in the code where it seems best and try it. Your computer won't blow up if it doesn't work.
The reason I suggest this rather than just give you the answer is because a vital part of learning to program is not being afraid to experiment, to play with your code.
Best of luck!
- 12-09-2008, 06:15 PM #14
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
thx 4 ur advice i did it!!!!
But my loop is a bit different with ur....
Pls tell me Y??
Java Code:import java.util.Scanner; public class guessgame { public static void main( String args[] ) {Scanner input=new Scanner(System.in); double a=1185188;//answer kherh double b=8211;//hua double c=19151514;//soon double d; double e; double f; System.out.print("Please guess who I love\nThe answer should given in code nomber"); System.out.print("\nPlease type your answer"); System.out.print("\nFirst name:"); d=input.nextDouble(); System.out.print("\nMiddle name:"); e=input.nextDouble(); System.out.print("\nEnd name:"); f=input.nextDouble(); if(a+b+c!=d+e+f) System.out.print("You are incorrect!!!!!\nPlease try again!!!!"); boolean done = false; while (a+b+c!=d+e+f) // while not done { System.out.print("\nFirst name:"); d=input.nextDouble(); System.out.print("\nMiddle name:"); e=input.nextDouble(); System.out.print("\nEnd name:"); f=input.nextDouble(); done=true; if(a+b+c!=d+e+f) System.out.print("You are incorrect!!!!!\nPlease try again!!!!"); done = false; } if(a+b+c==d+e+f) System.out.print("You are correct!!!!"); }}


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks