Results 1 to 3 of 3
  1. #1
    kath09 is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Two random numbers

    Hi,
    I writing a "Math Game program" which does the follow:
    Generate random two operand problems.
    Allow to the user to enter the solution
    Each time the user gets a correct result then display a random encouraging message. Similar for incorrect attempts.

    This program works good in BlueJ, BUT when I write those codes in NetBeans, My problems are:
    The two random operands always start with 0.
    If statement don't get the properly result.
    I set the For loop in 3, but it don't end.

    Any suggestions would be great.
    Thank you.


    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class Game extends Applet implements ActionListener {
       Label prompt;
       TextField input;
       int numb1;
       int numb2;
       int player;
       int answer;
       Label status;
       
       
        public void init() 
       {
         prompt = new Label( "What is " + numb1 + " + " + numb2 + "?" );
         add( prompt );
         
         input = new TextField( 10);
         add( input );
         
         input.addActionListener( this );
         
         status = new Label("Status");
         add(status);
       }
        
         public void paint (Graphics g) 
         {
          setBackground (Color.ORANGE);
         }
         
         public void actionPerformed( ActionEvent e )
        {
          for ( int i = 1; i<= 3; i++ )
          {
            numb1 = (int)(Math.random()*10)+1;
    
            numb2 = (int)(Math.random()*10)+1;
            
            prompt.setText( "What is " + numb1 + " + " + numb2 + "?");
            player = Integer.parseInt(e.getActionCommand());
            input.setText("");
            
            
            answer = (numb1 + numb2);  
            
            if (player == answer)
              { 
               status.setText("Excelent");
              }
            else 
            {
             status.setText("Incorrect answer, please try again");       
            }
          }
          
          repaint();
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Two random numbers

    I set the For loop in 3, but it don't end.
    Can you explain what happens and what you see when you execute the program.
    Are there any errors?

    Add some println statements to the code to show variable values and execution for to help you understand what your code is doing.

    The output for an applet will be in the browser's java console.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 02:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM

Tags for this Thread

Posting Permissions

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