[英]How do I regenerate a random number in a different class in java?
我对 Java 还是很陌生,所以如果我的问题看起来很明显,请原谅我......我正在制作一个随机侮辱生成器,它可以使用的所有单词“词汇表”都在名为 generate() 的 class 中。 我希望能够在一个名为 randomize() 的方法中从 generate() 中更改一个随机生成的数字,该方法是在另一个名为侮辱Generator() 的 class 中。 这是我的insultGenerator() class的代码:
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() class 代码:
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() 方法没有改变变量的值。 抱歉,如果我提供的代码超出了必要的范围,但我认为这总比不够好...提前致谢!
您的问题是,您在每种方法中都创建了 Generate object,最终它对在主 class 中创建的 Generate object 没有任何影响。 尝试将 main 方法中创建的 Generate object 传递给 Action class 并随机化
Randomize - method
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;
}
….
…
}
'从主创建动作'
button.addActionListener(new Action(g));
是的,您正在创建新的 generate 实例,而不是使用相同的 object。 您可以将 g 作为参数传递,但是将声明放在方法之外会给它们更多的 scope 以便可以在方法之外(从您的内部类)访问它们。 我不得不在 Java 201 课程中做这样的项目。
另外请记住 DRY 原则:不要重复自己。 意思是代码片段。 见下文,它仍然需要一些重构/重命名,但它解决了手头的问题,让你朝着更好的方向前进......
侮辱生成器:
import java.awt.Dimension;
import java.awt.event.*;
// you're not using Random here
//import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class insultGenerator {
// place the declarations of generate and insult here so that you can access in your inner class...
static generate g;
static JLabel insult;
public static void main(String args[]) {
// ...and create them here
g = new generate();
insult = new JLabel("", SwingConstants.CENTER);
// put your gui initializations in one spot, and they only get called once instead of after each button fire
g.frame.setLayout(null);
g.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g.frame.setPreferredSize(new Dimension(300, 200));
g.frame.setLocationRelativeTo(null);
g.frame.pack();
g.frame.setVisible(true);
insult.setBounds(0, 38, 300, 25);
g.frame.add(insult);
JButton button = new JButton("Generate New Insult");
button.setBounds(75, 100, 150, 40);
button.addActionListener(new Action());
g.frame.add(button);
// generate your first words
button.doClick();
}
static class Action extends generate implements ActionListener {
// here you were just creating a whole new g, NOT the same g that you are working with
//generate g = new generate();
public void actionPerformed(ActionEvent e) {
// what happens when the button is clicked
g.randomize();
if(g.beginning == 0) {
insult.setText(g.beginningList[g.beginning] + g.secondList[g.second] + g.fourthList[g.fourth]
+ g.thirdList[g.third] + g.endingList[g.ending]);
}
else {
insult.setText(g.beginningList[g.beginning] + g.firstList[g.first] + g.secondList[g.second]
+ g.fourthList[g.fourth] + g.thirdList[g.third] + g.endingList[g.ending]);
}
}
}
}
产生:
import java.util.Random;
import javax.swing.JFrame;
public class generate {
// declare and initialize variables
JFrame frame;
int beginning = 0, first = 0, second = 0, third = 0, fourth = 0, ending = 0;
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 = { ".", "!", "..." };
// create a constructor, it's good practice
public generate() {
frame = new JFrame("Insult Generator");
}
// Just call this method to generate new numbers/words.
// Cleaned up unnecessary extra objects, just use the same instance of Random.
public void randomize() {
Random random = new Random();
// automatically get the size, so when you add new words to the list you don't need to edit here
beginning = random.nextInt(beginningList.length);
first = random.nextInt(firstList.length);
second = random.nextInt(secondList.length);
third = random.nextInt(thirdList.length);
fourth = random.nextInt(firstList.length);
ending = random.nextInt(endingList.length);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.