Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Need help ASAP!!!!

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help ASAP!!!!

    I dont know where i need to put a loop at in my code to ask a new addition problem.
    This program is for some kids between the ages of 4-7. Please help!!!!

    import java.util.Random;
    import javax.swing.JOptionPane;

    public class Adding_Problems{
    public static void main(String [] args) {

    //Declare
    Random generator = new Random();
    int num1;
    int num2;
    int answer;
    int guess1;
    String choice;
    boolean shouldLoopEnd = false;//boolean variable to determine if the loop should end

    //Input
    num1 = generator.nextInt(50); //Gets random num1
    num2 = generator.nextInt(50); //Gets Random num2

    //infinite loop begins...
    while (true)
    {

    choice=JOptionPane.showInputDialog ("what is " + num1 + "+" + num2);


    //Process
    guess1 = Integer.parseInt(choice);
    answer = (num1 + num2);


    //Output

    if (guess1 == answer){ //check to see if the guess is correct
    shouldLoopEnd = true;}

    if(shouldLoopEnd == true)
    { //Begin if bracket
    JOptionPane.showMessageDialog (null, "Correct!");
    break; // break loop
    } //End if Brackets
    else
    {
    //around the loop we go again

    JOptionPane.showMessageDialog (null,"Guess again");
    }
    }
    }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Need help ASAP!!!!

    Hi Swiper.
    Firstly, please don't emphasise urgency in your thread names, your thread is no more important than anyone else's so stick to informative titles instead.
    Secondly, always surround your code in tags such as the ones found in my signature.
    And lastly, all you need to do is move
    num1 = generator.nextInt(50); //Gets random num1
    num2 = generator.nextInt(50); //Gets Random num2
    inside your while loop, and remove break from inside the winning if statement.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help ASAP!!!!

    if i do that then it messes up my loop for when the answer is wrong

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Need help ASAP!!!!

    How does it? What's the problem that's occurring?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help ASAP!!!!

    when i type in the wrong answer it says correct and gives me two new numbers. I need it to say Guess Again and go back to that same equation

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Need help ASAP!!!!

    Then add shouldLoopEnd = false; to the start of the while loop
    Move the number generation into the winning If statement.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help ASAP!!!!

    where would I add that

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Need help ASAP!!!!

    Move the number generation into the winning If statement.
    into the winning If statement
    winning If statement
    Initialise the original numbers before the while loop, then if the person guesses correctly, generate new numbers in the if block.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. The Following User Says Thank You to newbie For This Useful Post:

    Swiper (August 12th, 2011)

  10. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help ASAP!!!!

    can you be more specific

  11. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Need help ASAP!!!!

    Apart from coming over to your house and typing it for you, No I don't think I can.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. GUI - calculate BMI (need help asap)
    By jahead in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2011, 04:10 AM
  2. NEED HELP ASAP!!
    By JavaStudent_09 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 18th, 2010, 07:33 PM
  3. NEED HELP ASAP PLEASE!!!
    By mbm4ever in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 12th, 2010, 07:01 PM
  4. Need a calandar app done asap
    By gixmo in forum Paid Java Projects
    Replies: 6
    Last Post: July 10th, 2010, 08:58 PM
  5. Recursion Problem Need Help ASAP
    By Delstateprogramer in forum Algorithms & Recursion
    Replies: 6
    Last Post: June 26th, 2010, 08:36 PM