所以我正在尝试创建一个程序,当我点击我的Skate或骰子按钮时,每个标签(代表一个骰子)都会从它所属的相应列表中变为随机元素。但到目前为止,当我单击按钮时,标签不会更改为随机元素,每次单击它时它都会给我相同的标签。当我点击按钮时,我通过将输出打印到系统来测试输出,并且我每次都得到相同的4个答案。我想帮助弄清楚为什么每次重复从每个列表中选择相同的元素而不是每次都不同的元素。
这是我的主要课程
package skateordicegui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SKFrame extends SkateDice {
public JFrame jf;
public JPanel jp;
static public JButton sod = new JButton("SKATE OR DICE!!");
static public JLabel l1 = new JLabel("Stance");
static public JLabel l2 = new JLabel("Turn");
static public JLabel l3 = new JLabel("Flip");
static public JLabel l4 = new JLabel("Spin");
public SKFrame() {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
jf = new JFrame();
jf.setSize((d.width / 2), (d.height / 2));
jf.setLocation((d.width / 2 - d.width / 4 ), (d.height / 2 - d.height / 4));
jf.setMinimumSize(jf.getSize());
jf.setResizable(true);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1.setBounds(110, 150, 100, 40);
l2.setBounds(230, 150, 100, 40);
l3.setBounds(350, 150, 100,40);
l4.setBounds(470, 150, 100,40);
sod.setLocation(140, 20);
sod.setSize(300, 100);
jp = new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.CENTER));
jp.setSize(jf.getWidth(), jf.getHeight() / 2);
jp.add(l1);
jp.add(l2);
jp.add(l3);
jp.add(l4);
jp.add(sod);
jf.add(jp);
sod.addActionListener(action);
}
public ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == sod) {
System.out.println(SkateDice.getStance());
System.out.println(SkateDice.getTurn());
System.out.println(SkateDice.getFlip());
System.out.println(SkateDice.getSpin());
l1.setText(SkateDice.getStance());
l2.setText(SkateDice.getTurn());
l3.setText(SkateDice.getFlip());
l4.setText(SkateDice.getSpin());
} else
System.out.println("no swag");
}
};
public static void main(String[] args) {
SKFrame skf = new SKFrame();
}
}
这是我的SkateDice课程
package skateordicegui;
import java.util.Random;
public class SkateDice {
static Random r = new Random();
// First time dealing with arrays
//this is the easy way (kinda like python)
static String[] d1 = {"Kickflip", "Kickflip", "Kickflip", "Heelflip", "Heelflip","Heelflip", "Shuv", "3 Shuv", "Shuv", "3 Shuv", "X", "Skate Or Dice"};
static String[] d2 = {"180", "180", "180", "360", "360", "360", "X", "Skate Or Dice"};
//OR
static String d3[] = {"FS", "FS", "BS", "BS", "FS", "BS", "X", "Skate Or Dice"};
static String d4[] = {"Regular", "Regular", "Regular", "Switch", "Switch", "Switch", "Nollie", "Nollie", "Nollie", "Fakie", "Fakie", "Fakie", "X", "Skate Or Dice"};
public static int r1 = r.nextInt(d1.length);
public static int r2 = r.nextInt(d2.length);
public static int r3 = r.nextInt(d3.length);
public static int r4 = r.nextInt(d4.length);
public static String flip = d1[r1];
public static String spin = d2[r2];
public static String turn = d3[r3];
public static String stance = d4[r4];
static public String getFlip() {
return flip;
}
static public String getSpin() {
return spin;
}
static public String getTurn() {
return turn;
}
static public String getStance() {
return stance;
}
}
答案 0 :(得分:0)
当你打电话给你的getters时,他们只是按预期返回翻转,旋转,转弯和站姿的值。如果您每次调用它们时都需要它们来更改值,那么您应该实现以下内容:
static public String getFlip() {
// generates a new random number
r1 = r.nextInt(d1.length);
// set a new value to flip
flip = d1[r1];
// return the value
return flip;
}
对其他的getter重复这个逻辑,你的程序应该可以工作。