Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Generate two random numbers

kath09kath09 Member Posts: 2
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.

[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");

public void paint (Graphics g)
setBackground (Color.ORANGE);

public void actionPerformed( ActionEvent e )
for ( int i = 1; i<= 3; i++ )<br /> {
numb1 = (int)(Math.random()*10)+1;

numb2 = (int)(Math.random()*10)+1;

prompt.setText( "What is " + numb1 + " + " + numb2 + "?");
player = Integer.parseInt(e.getActionCommand());

answer = (numb1 + numb2);

if (player == answer)
status.setText("Incorrect answer, please try again");



Sign In or Register to comment.