gpt4 book ai didi

java - 如何在java中的不同类中重新生成随机数?

转载 作者:行者123 更新时间:2023-12-02 09:17:37 25 4
gpt4 key购买 nike

我对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com