我必须有一个随机数生成器,它从用户那里获取一个数字,然后在 1 和用户数之间生成 10000 个随机数,然后计算最小值、最大值和平均值。这是我到目前为止所拥有的。我被困在 actionPerformed 方法上。我是个菜鸟,所以请尝试解释您的答案。
我的问题
我有 JApplet 编码只是无法弄清楚执行的操作方法。我在谷歌上下搜索了帮助以及本周本书附录 c 中涵盖的章节,他们没有解释如何做我们被要求做的事情。我已经自己完成了课堂上的所有其他作业,但似乎无法完成这项作业,到目前为止已经花了 19 个小时。
如何将所有数字添加到数组列表中?
package randomNums;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.lang.Math.*;
import javax.swing.*;
import java.applet.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
public class RandomNums extends Applet implements ActionListener {
/**
*
*/
// PAINT METHOD
public void paint(Graphics g)
{
Font font = new Font("Arial", Font.BOLD, 18);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString("Enter A Number", 70, 25);
resize(350, 350);
this.setBackground(Color.BLUE);
}
// CREATES OBJECTS
private static final long serialVersionUID = 1L;
TextField text1, text2, text3, text5;
Label label1, label2, label3, label4;
Button button;
Font font = new Font("Arial", Font.BOLD, 11);
private double all;
// INIT METHOD
public void init() {
setLayout(null);
repaint();
// YOUR NUMBER LABEL
label1 = new Label("Your Number ");
label1.setBounds(25, 35, 100, 20);
setFont(font);
add(label1);
// YOUR NUMBER ENTRY
text1 = new TextField(5);
text1.setBounds(150, 30, 100, 25);
add(text1);
// MAXIMUM
label2 = new Label("The Maximum Number Is: ");
label2.setBounds(25, 100, 150, 25);
setFont(font);
add(label2);
// MAXIMUM ANSWER
text2 = new TextField(5);
text2.setBounds(180, 100, 50, 25);
add(text2);
// MINIMUM
label3 = new Label("The Minimum Number Is: ");
label3.setBounds(25, 170, 150, 25);
setFont(font);
add(label3);
// MINIMUM ANSWER
text5 = new TextField(5);
text5.setBounds(180, 170, 50, 25);
add(text5);
// MEAN
label4 = new Label("The Mean is: ");
label4.setBounds(25, 135, 150, 25);
setFont(font);
add(label4);
// MEAN ANSWER
text3 = new TextField(5);
text3.setBounds(180, 135, 50, 25);
add(text3);
// BUTTON
button = new Button("Enter");
button.setBounds(90, 70, 100, 20);
add(button);
// ACTION LISTENER
button.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
Random ran = new Random(10000);
try {
//NOT SURE HOW TO GET ALL OF THE NUMBERS ADDED TO THE ARRAY LIST
double[] arrList = ran();
//THIS IS NOT WORKING RIGHT ONLY STORING ONE VALUE
all = ran.nextDouble();
for (int i = 0; i < arrList.length; i++) {
System.out.println(arrList[i] + " ");
//THIS IS IN THERE FOR MY TESTING PURPOSES NEED TO TAKE OUT BEFORE SUBMITTING
System.out.println(arrList);
final double TIMES = (double) 10000;
final String LIMIT = text1.getText();
Double.parseDouble(LIMIT);
//FOR LOOP
for (int x = 1; x < TIMES; ++x);
//SETS TEXT FOR MIN BOX (NOT SURE IF IT IS DOING THE CALCULATIONS RIGHT)
text5.setText(ran.nextDouble() + "");
//my comment: another variable after for loop to get mean , fix numbers being saved to an array so they can be added and divided to get mean,
}
}
catch (NumberFormatException m) {
if (getText(text1) == 0)
JOptionPane.showMessageDialog(this,
"Please enter a number between 1- 10,000");
}
}
private int getText(TextField text12) {
// TODO Auto-generated method stub
return 0;
}
}