![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
I wonder if I could find any help with a problem I have regarding my latest program...
Im trying to generate a number at random between 200 & 400 and also display the average everytime a button is clicked, so the average will rise and fall but in theory eventually converge on or around 300. So far my program only generates a number between 0 & 400 (not 200 & 400 as I need it to) and I havent any ideas for how to show an average after every click Any help please? import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RandomNumber extends JFrame implements ActionListener {
private JButton button;
private JTextField valuesTextField, resultTextField;
private Random random;
public static void main(String[] args) {
RandomNumber frame = new RandomNumber();
frame.setSize(200,150);
frame.createGUI();
frame.show();
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
button = new JButton("Generate");
window.add(button);
button.addActionListener(this);
valuesTextField = new JTextField(14);
window.add(valuesTextField);
random = new Random();
}
public void actionPerformed(ActionEvent event) {
int one;
one = random.nextInt(400) + 1;
valuesTextField.setText("Number: "
+ Integer.toString(one)); }
}
__________________
-Hadrurus Even if the voices aren't real, they have some good ideas. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
Well, you're asking it for a number between a number between 1 and 400, so that's what it's going to give you. To get a number between a and b, you'll want to do this:
random_number = random.nextInt(b - a) + a + 1; |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
Use this for the random number:
one = 200 + random.nextInt(200);
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 Last edited by skuinders; Aug 15th, 2005 at 10:38 AM. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
Here's a simple implementation of what you want:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RandomNumber extends JFrame implements ActionListener {
private JButton button;
private JTextField valuesTextField, resultTextField, meanTextField;
private Random random;
private int total = 0;
private int count = 0;
public static void main(String[] args) {
RandomNumber frame = new RandomNumber();
frame.setSize(200,150);
frame.createGUI();
frame.show();
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
button = new JButton("Generate");
window.add(button);
button.addActionListener(this);
valuesTextField = new JTextField(14);
window.add(valuesTextField);
meanTextField = new JTextField(14);
window.add(meanTextField);
random = new Random();
}
public void actionPerformed(ActionEvent event) {
int r;
count+=1;
r = 200 + random.nextInt(200);
total+=r;
valuesTextField.setText("Number: " + Integer.toString(r));
meanTextField.setText("Mean: " + Double.toString((double)total/count));
}
}
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 |
|
|
|
|
|
#5 |
|
Newbie
|
Thanks Ooble and Skuinders both your help much appreciated
The program works perfectly now.thanks again, ![]()
__________________
-Hadrurus Even if the voices aren't real, they have some good ideas. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
Speaking of a random number, how would you get a random number in C? I'm curious about manipulating the range of the random number too.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|