Results 1 to 12 of 12
Like Tree4Likes
  • 1 Post By Swiper
  • 1 Post By sunde887
  • 1 Post By sunde887
  • 1 Post By sunde887

Thread: Need help ASAP on a math program

  1. #1
    Swiper is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default Need help ASAP on a math program

    I am writing a math program that is just adding numbers. I am having a problem when it comes to if the put the wrong answer that it goes back to the question. I also need to figure out how to loop it back to generate new numbers after they answer the problem correctly. here is my code
    Java Code:
    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
          
             choice=JOptionPane.showInputDialog ("what is " + num1 + "+" + num2);
          
          
          //Process
             guess1 = Integer.parseInt(choice);
             answer = (num1 + num2);
          
    		//infinite loop begins...
             while (true) 
             {
             
             //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");
                } 
             } 
          }
       }
    Last edited by sunde887; 08-12-2011 at 09:24 PM. Reason: Code tags added, [code]...[/code]
    JeffGrigg likes this.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Well, you will want a nested loop. When you get into nested loops, often times it's easier to create multiple methods and break one nested loop to a few method calls.

    Java Code:
    public static void main(String[] args){
      while(true){
        while(true){
          while(true){ ... }
        }
      }
    }
    Instead of this, it will be easier to manage something which does
    Java Code:
    public void startLoopInner(){
      while(true){...}
    }
    public void startMidLoop(){
      while(true){
        startLoopInner();
      }
    }
    public static void main(String[] args){
      while(true){
        startMiddleLoop();
      }
    }
    This snippet isn't entirely correct, it's meant to show the idea.

    Perhaps it will be useful to create a method which asks the question, and gets the a single answer, then a method which has a loop which repeats this question until a correct answer is given. Finally in main you would just have one loop, which asks generates the numbers, then begins the loop. Working with the smaller pieces and making them interact should make this easier to maintain and think about.

    When comparing booleans in some statement which takes a boolean expression(if,while,etc) it is unnecessary to do
    Java Code:
    booleanVar == true
    or the same for testing for false. The == returns a boolean, so it is no different then simply passing in the booleanVar instead you should test for truth with
    Java Code:
    if(booleanVar)
    and falsehood with
    Java Code:
    if(!booleanVar)

    Your while(true) loop will never end, it tells you to guess again, but doesn't actually get new input.
    Last edited by sunde887; 08-12-2011 at 09:34 PM.
    JeffGrigg likes this.

  3. #3
    Swiper is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    So where do i need to put that like im still confused on that part

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Perhaps taking an oo approach may be helpful? You may want to check out Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language)

    If you don't, try breaking this into steps. One step could get user input and check if the answer matches the input. From there you will basically do the following

    Java Code:
    loop
      generate numbers
      do
        get input and test it
      while
    end loop
    This is a rough outline of what you probably want. This has one loop that gets user input until the correct answer is guessed, and one loop to generate new numbers and repeat the process again.
    JeffGrigg likes this.

  5. #5
    Swiper is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    well the program is for kids that are like the ages of 4-6

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Is it for them to write, or use?
    JeffGrigg likes this.

  7. #7
    Swiper is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    it is for them to use

  8. #8
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    OK; the first thing you need to do is to move this code...
    Java Code:
    		// infinite loop begins...
    		while (true) {
    up to be after where the numbers to add are generated...
    Java Code:
    		num1 = generator.nextInt(50); // Gets random num1
    		num2 = generator.nextInt(50); // Gets Random num2
    and before where you ask the user to enter their guess:
    Java Code:
    		choice = JOptionPane.showInputDialog("what is " + num1 + "+" + num2);
    In other words, what you want to do every single time is to ask the question. But what you do not want to do every time is to come up with two new numbers to add.

    Once you have this working, putting in more loops is another issue...

  9. #9
    Swiper is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    ok so that helped me reasked the question so now all i have to do is get it to ask a new question

  10. #10
    Swiper is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    can anybody help me out now this is the new code now that somethings changed
    Java Code:
    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");
                } 
             } 
          }
       }
    Last edited by sunde887; 08-13-2011 at 12:03 AM. Reason: Code tags added, [code]...[/code]

  11. #11
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    sunde887 gave you several really good suggestions. I suggest you reread them and consider them carefully.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If using oop thinking is a bit above your level, you can just use static methods to break it up as well. While this can be accomplished in just the main method, it's harder to think about. When clumping everything into main, you need to be conscious of what everything is doing and how it works together. When using multiple methods(static of non), you allow yourself to think of individual pieces separately, which leads to an easier time designing the code. Another thing that I find useful is to write out what you want the code to do in English and as much detail as possible.

    For example:(in regards to your code)

    This code does the following, first it generates two random numbers and sums them up to form the answer. The answer is the number the player will be attempting to guess. After the answer is calculates the code will ask the user for some input. With this input the code will determine.....

    I'll let you finish the rest. The more detail you can pour into this description, the easier it will be to translate to the actual code. The goal of this description is to outline what the method does in as much detail as possible. This program is fairly trivial, however; the description process I explained above can be valuable as you move to more complex programs. Stepping outside of the system(the programming language), and thinking about the program in easier to understand terms is a great approach. With other programs it could also be helpful to work out by hand how you would do something, then translating it to java(think fibb numbers, factorial, etc).

    While it seems challenging, this problem is much simpler than it needs to be. I would recommend something like this for your final code

    Java Code:
    public class Guess{
      private int num1;
      private int num2;
      private int choice;
      private int answer;
    
      public static final RNG = new Random();
      
      public Guess(){ //constructor }
      public void newGame(){//generate new information }
      public void getPlayerInput(){
        //get player input and set the choice variable from here
      }
      public static void main(String[] args){
        Guess game = new Guess();
        game.methods(); //call methods on the game
      }
    }
    There will be other methods you will want to use as well. This is just a brief "template".

Similar Threads

  1. Help with a PolyGrapher Program ASAP
    By acole5 in forum New To Java
    Replies: 6
    Last Post: 06-15-2011, 03:35 AM
  2. HELP with a plotter program ASAP!
    By acole5 in forum New To Java
    Replies: 3
    Last Post: 06-03-2011, 12:16 AM
  3. Assistance needed ASAP: Postpix program
    By Debonairj in forum New To Java
    Replies: 18
    Last Post: 07-27-2010, 01:37 PM
  4. Need help ASAP with Payroll Program Part 2
    By arrech326 in forum New To Java
    Replies: 10
    Last Post: 11-17-2009, 10:17 PM
  5. need help with math for a new program
    By gotenks05 in forum New To Java
    Replies: 13
    Last Post: 09-26-2008, 07:32 AM

Posting Permissions

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