我目前所拥有的:在方法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
注意括号"{''}"我认为您在actionPerformed
、BigArray
和createGUI()
下缺少一个。
这样编码可能很有帮助:
class myClass
{
int myInt;
public void setInt(int myInt)
{
this.myInt = myInt;
}
}
每个右大括号都位于起始大括号下方。