作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Java 还很陌生,所以如果我的问题看起来很明显,请原谅我......我正在制作一个随机侮辱生成器,它可以使用的所有单词“词汇”都在一个名为“generate”的类中()。我希望能够在不同类中的 randomize() 方法中更改从generate() 中随机生成的数字,该方法名为insulmGenerator()。这是侮辱生成器()类的代码:
import java.awt.Dimension;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class insultGenerator {
public static void main(String args[]){
generate g = new generate();
g.frame.setLayout(null);
g.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Generate New Insult");
button.setBounds(75, 100, 150, 40);
button.addActionListener(new Action());
g.frame.add(button);
g.frame.setPreferredSize(new Dimension(300, 200));
g.frame.setLocationRelativeTo(null);
g.frame.pack();
g.frame.setVisible(true);
if(g.beginning == 0) {
JLabel insult = new JLabel((g.beginningList[g.beginning] + g.secondList[g.second] + g.fourthList[g.fourth] + g.thirdList[g.third] + g.endingList[g.ending]),SwingConstants.CENTER);
insult.setBounds(0, 38, 300, 25);
g.frame.add(insult);
} else {
JLabel insult = new JLabel((g.beginningList[g.beginning] + g.firstList[g.first] + g.secondList[g.second] + g.fourthList[g.fourth] + g.thirdList[g.third] + g.endingList[g.ending]),SwingConstants.CENTER);
insult.setBounds(0, 38, 300, 25);
g.frame.add(insult);
}
}
static class Action extends generate implements ActionListener {
generate g = new generate();
public void actionPerformed(ActionEvent e) {
//what happens when the button is clicked
randomize();
if(g.beginning == 0) {
JLabel insult = new JLabel((g.beginningList[g.beginning] + g.secondList[g.second] + g.fourthList[g.fourth] + g.thirdList[g.third] + g.endingList[g.ending]),SwingConstants.CENTER);
insult.setBounds(0, 38, 300, 25);
g.frame.add(insult);
} else {
JLabel insult = new JLabel((g.beginningList[g.beginning] + g.firstList[g.first] + g.secondList[g.second] + g.fourthList[g.fourth] + g.thirdList[g.third] + g.endingList[g.ending]),SwingConstants.CENTER);
insult.setBounds(0, 38, 300, 25);
g.frame.add(insult);
}
}
}
public static void randomize() {
//regenerate the random numbers in generate()
generate g = new generate();
g.beginning = g.begin.nextInt(2);
g.first = g.one.nextInt(3);
g.second = g.two.nextInt(5);
g.third = g.three.nextInt(23);
g.fourth = g.four.nextInt(14);
g.ending = g.end.nextInt(3);
}
}
这是我的generate() 类的代码:
import java.util.Random;
import javax.swing.JFrame;
public class generate {
JFrame frame = new JFrame("Insult Generator");
Random begin = new Random();
int beginning = begin.nextInt(2);
Random one = new Random();
int first = one.nextInt(3);
Random two = new Random();
int second = two.nextInt(5);
Random three = new Random();
int third = three.nextInt(23);
Random four = new Random();
int fourth = four.nextInt(14);
Random end = new Random();
int ending = end.nextInt(3);
String[] beginningList = {"You ", "Your "};
String[] firstList = {"parents ", "friends ", "grandparents "};
String[] secondList = {"are ", "smell ", "look ", "sound ", "act "};
String[] thirdList = {"bad", "dumb", "ugly", "stupid", "fat", "terrible", "crappy", "poopy", "weird", "horrible", "atrocious", "awful", "gross", "substandard", "yucky", "unacceptable", "smelly", "rancid", "annoying", "disturbing", "creepy", "idiotic", "lame"};
String[] fourthList = {"", "very ", "really ", "extremely ", "pretty ", "actually ", "quite ", "too ", "totally ", "strikingly ", "immensely ", "so ", "way too ", "kinda "};
String[] endingList = {".", "!", "..."};
}
同样,问题是 randomize() 方法没有更改变量的值。抱歉,如果我提供的代码多于必要的代码,但我认为这总比不够好......提前致谢!
最佳答案
您的问题是,您在每个方法中创建生成对象,最终它并没有对主类中创建的生成对象产生任何影响。尝试将 main 方法中创建的生成对象传递给 Action 类并随机化
随机化 - 方法
public static void randomize(Generate g) {
//reGenerate the random numbers in Generate()
g.beginning = g.begin.nextInt(2);
g.first = g.one.nextInt(3);
g.second = g.two.nextInt(5);
g.third = g.three.nextInt(23);
g.fourth = g.four.nextInt(14);
g.ending = g.end.nextInt(3);
}
Action
static class Action extends Generate implements ActionListener {
Generate g = null;
public Action(Generate g) {
this.g = g;
}
….
…
}
“从 main 创建操作”
button.addActionListener(new Action(g));
关于java - 如何在java中的不同类中重新生成随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58887315/
所以我有这个 UltraTicTacToe 游戏,我正在用 HTML/CSS/JS 编码。它由表中表中的表组成。当您单击 X 或 O 时,我想要突出显示您应该进入的下一个 TicTacToe 牌 ta
Some text Some more text 如何让每个 .example 的 .whatever 包含其 .test 的内容? 例如,如果代码是这样的: Som
我是一名优秀的程序员,十分优秀!