Results 1 to 3 of 3
Thread: Two random numbers
- 11-12-2011, 08:22 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
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(); } }
- 11-12-2011, 10:24 PM #2
Re: Two random numbers
Can you explain what happens and what you see when you execute the program.I set the For loop in 3, but it don't end.
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.
- 11-13-2011, 06:11 PM #3
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Help with random numbers
By checkmylongboarding in forum New To JavaReplies: 2Last Post: 01-12-2009, 05:47 AM -
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks