我目前所拥有的:在方法 fillArray 及其上面的 if 语句下有红线(错误)。目的是创建一个数组,该数组将从单击按钮开始,并用 0 到 100 之间的随机整数填充该数组
import java.awt.*; //imports data from library
import java.awt.event.*;
import javax.swing.*;
public class FinalArray extends JFrame implements ActionListener {
private JButton fill,
private JTextArea output;
public static void main(String[] args) { //creates window
FinalArray demo = new FinalArray();
demo.setSize(400,450);
demo.createGUI();
demo.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
fill = new JButton("fill");//creates button
window.add(fill); //and text area
fill.addActionListener(this);
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == fill) {
BigArray.fill();
}
class BigArray {
private int [] array;
public void fillArray() {
for(int i = 0; i < array.length; i++) {
array.fill(array[i]=0);
}
Random = new Random(101);
最佳答案
您可以传递给 Random
构造函数的参数是种子,而不是生成值的范围。 (种子是某种初始值;如果始终使用相同的种子,随机生成器将始终生成相同的数字序列)。
如果你想获得一定范围内的随机数,请使用random.nextInt(int)
方法:
int a = random.nextInt(101); // returns a random value between 0 and 100
关于java - 如何用随机整数填充数组并创建一个按钮来激活它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20337253/