Results 1 to 14 of 14

Thread: help me plss

  1. #1
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Exclamation 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Twitchy5's Avatar
    Twitchy5 is offline Member
    Join Date
    Dec 2008
    Posts
    13
    Rep Power
    0

    Default

    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

  4. #4
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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
    }
    can u explaine the concept 2 me???coz i new and i juz learn how to use if statement

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Twitchy5 View Post
    Maybe put it into a while loop, and use the continue command if you get it wrong and break if you get it right.
    I'm not sure that a break is needed here, and I'm darn sure that a continue command would be bad here.

  7. #7
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    Finally thx 4 ur help.The code work

  8. #8
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    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 class
    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.
    Last edited by low224; 12-09-2008 at 06:36 AM.

  9. #9
    Rajesh_J2EE is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    What you could probably use is switch case with terinary operators.

  10. #10
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    :eek:
    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!!!!");
    	}
    		}}
    Well the previous problem i had solve with the code above.
    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.

  11. #11
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    once it's correct just set done to true

  12. #12
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    the done=true is put inside the loop statement or outside the loop statement???

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  14. #14
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Exclamation

    Quote Originally Posted by Fubarable View Post
    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!
    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!!!!");
    		
    		}}

Posting Permissions

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