随机不会重复最后一个随机的随机
我正在使用Java Swing中的一个简单程序,该程序在您单击按钮时从数组中随机选择一个单词。问题是我不想在再次单击按钮后产生相同的内容。例如,您单击该按钮,然后从数组中生成“甜甜圈”一词,然后再次单击,然后再次生成甜甜圈。我想避免在之后直接生成同一事物。
这是代码:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
class Main implements ActionListener{
JFrame f = new JFrame("ℂ
I am working on a simple program in Java swing that randomly selects a word from an array when you click a button. The issue is that I do not want to have the same thing generated after clicking the button again. For example, You click the button and it generates the word ‘donut’ from the array, you click again, and it generates donut again. I want to avoid generating the same thing directly after.
Here is the code:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
class Main implements ActionListener{
JFrame f = new JFrame("ℂ???????????????????????????????? ????????????????????????????????????");
Random r = new Random();
JButton generate = new JButton("Generate");
JButton clear = new JButton("Clear");
JLabel characterClass = new JLabel("Style", JLabel.CENTER);
JLabel characterClass2 = new JLabel("Race", JLabel.CENTER);
JLabel characterClass3 = new JLabel("Weapon", JLabel.CENTER);
JLabel newGenerate = new JLabel("?", JLabel.CENTER);
JLabel newGenerate2 = new JLabel("?", JLabel.CENTER);
JLabel newGenerate3 = new JLabel("?", JLabel.CENTER);
Color custBlue = new Color(10, 63, 250);
Color custOrange = new Color(251,167,88);
Color saturatedCustOrange = new Color(254, 127, 46);
ImageIcon wheel = new ImageIcon("images/loading_wheel.gif");
JLabel img = new JLabel(wheel);
ImageIcon back_image = new ImageIcon("images/back_ground.png");
JLabel img2 = new JLabel(back_image);
String[] style = {"<html><p align='center'>Steampunk</p></html>", "<html><p align='center'>Sci-Fi</p></html>", "<html><p align='center'>Medieval</p></html>", "<html><p align='center'>Imprisoned</p></html>", "<html><p align='center'>Superpowered</p></html>", "<html><p align='center'>Villainous</p></html>", "<html><p align='center'>Primal</p></html>", "<html><p align='center'>Musical</p></html>", "<html><p align='center'>Aquatic</p></html>", "<html><p align='center'>Undead</p></html>"};
String[] race = {"<html><p align='center'>Sorcerer</p></html>", "<html><p align='center'>Alien</p></html>", "<html><p align='center'>Knight</p></html>", "<html><p align='center'>Orc</p></html>", "<html><p align='center'>Elf</p></html>", "<html><p align='center'>Bear</p></html>", "<html><p align='center'>Human</p></html>", "<html><p align='center'>Vampire</p></html>", "<html><p align='center'>Werewolf</p></html>", "<html><p align='center'>Dwarf</p></html>"};
String[] weapon = {"<html><p align='center'>Bow<br />And Arrow</p></html>", "<html><p align='center'>Blade</p></html>", "<html><p align='center'>Gun</p></html>", "<html><p align='center'>Nunchucks</p></html>", "<html><p align='center'>Shield</p></html>", "<html><p align='center'>Bomb</p></html>", "<html><p align='center'>Claws</p></html>", "<html><p align='center'>Mace</p></html>", "<html><p align='center'>Axe</p></html>", "<html><p align='center'>Staff</p></html>"};
public static void main(String[] args) {
Main o = new Main();
o.gui();
}
public void gui(){
f.setSize(500, 400);
this.newGenerate.setBounds(132,107, 180, 40);
newGenerate.setFont(new Font("Arial", Font.BOLD, 20));
this.newGenerate.setForeground(saturatedCustOrange);
f.add(this.newGenerate);
this.newGenerate2.setBounds(304,107, 180, 40);
newGenerate2.setFont(new Font("Arial", Font.BOLD, 20));
this.newGenerate2.setForeground(saturatedCustOrange);
f.add(this.newGenerate2);
this.newGenerate3.setBounds(218,250, 180, 40);
newGenerate3.setFont(new Font("Arial", Font.BOLD, 20));
this.newGenerate3.setForeground(saturatedCustOrange);
f.add(this.newGenerate3);
characterClass.setBounds(132,50, 180, 40);
characterClass.setFont(new Font("Arial", Font.BOLD, 20));
this.characterClass.setForeground(saturatedCustOrange);
f.add(this.characterClass);
characterClass2.setBounds(304,50, 180, 40);
characterClass2.setFont(new Font("Arial", Font.BOLD, 20));
this.characterClass2.setForeground(saturatedCustOrange);
f.add(this.characterClass2);
characterClass3.setBounds(218,192, 180, 40);
characterClass3.setFont(new Font("Arial", Font.BOLD, 20));
this.characterClass3.setForeground(saturatedCustOrange);
f.add(this.characterClass3);
this.generate.setBounds(50, 240, 120, 40);
generate.setFont(new Font("Arial", Font.BOLD, 16));
this.generate.setForeground(custOrange);
this.generate.setBackground(custBlue);
this.generate.addActionListener(this);
f.add(this.generate);
this.clear.setBounds(50, 290, 120, 40);
clear.setFont(new Font("Arial", Font.BOLD, 16));
this.clear.setForeground(custOrange);
this.clear.setBackground(custBlue);
this.clear.addActionListener(this);
f.add(this.clear);
img.setBounds(0, -10,500,400);
f.add(img);
img2.setBounds(0, -10,500,400);
f.add(img2);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == this.clear){
img.setVisible(true);
this.newGenerate.setText("?");
this.newGenerate2.setText("?");
this.newGenerate3.setText("?");
}
else{
this.newGenerate.setText(this.returnStyle());
this.newGenerate2.setText(this.returnRace());
this.newGenerate3.setText(this.returnWeapon());
img.setVisible(false);
}
}
public String returnStyle() {
int ranNum = r.nextInt(this.style.length);
return this.style[ranNum];
}
public String returnRace() {
int ranNum = r.nextInt(this.race.length);
return this.race[ranNum];
}
public String returnWeapon() {
int ranNum = r.nextInt(this.weapon.length);
return this.weapon[ranNum];
}
}
If it is confusing, the program generates a character. It randomly mixes the style of character (like medieval or sci-fi), a race (like human or vampire), and a weapon (like a blade or axe).
I specifically need help with this portion, and I preferably need to do it with a loop, since it is the only way I could think to work a loop into this (it is required for my final project.)
public String returnStyle() {
int ranNum = r.nextInt(this.style.length);
return this.style[ranNum];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于“不同的”方法,您可以使用
集合#shuffle
随机alist> list
值首先需要,您将需要...
接下来,您将需要定义一系列“随机”
list
s。这允许我们在维护对“原始”值的引用时获得下一个随机值的更简单方法,例如...然后,您将修改
return> returnxxx
方法以返回下一个随机值,除了,如果随机list
isempty
,则用原始值填充,然后shuffle
it ...这可以保证值为值。从
列表中,只有一旦您使用了原始
才会开始重复,list
的所有值,然后我们可以将概念封装到类中,以使其更容易。 ..
哪个将您的代码更改为...
和您的
returnxxx
方法,例如……更简单,更干净,并删除所有重复的代码...但是,这可能是比您现在更加“高级”,但是要记住的事情
For a, "different" approach, you could make use of
Collections#shuffle
to randomise aList
of valuesFirst, you will need...
Next, you will need define a series of "random"
List
s. This allows us a simpler method for getting the next random value while maintaining a reference to the "original" values, for example...Then, you would modify your
returnXxx
methods to return the next random value, except, if the randomList
isEmpty
, you fill it with the original values and thenshuffle
it, for example...This guarantees that values from the
List
will only begin to repeat once you have used ALL the values from the originalList
We could then encapsulate the concept into a class to make it easier, for example...
Which would change your code to something like...
and your
returnXxx
methods to something like...which is much simpler and cleaner and removes all the duplicate code ... but, this might be a bit more "advanced" then you are right now, but something to keep in mind ????
基本上您可以做这样的事情:
创建一个列表。
生成一个随机索引,将元素弹出此索引,重复。
Basically you can do something like this:
create a list.
generate a random index, pop the element in this index, repeat.
如果您希望它能够重复结果,而不是它产生的最后一个结果,则可以存储其产生的最后一个结果,并检查新的结果是否相同。如果是,请生成另一个。以ATP的答案为例并修改以实现此目的,如下所示:
如果您不希望它重复任何结果,并且仅生成尽可能多的结果,那么ATP的答案将执行诡计。
If you want it to be able to repeat results, just not the last one it produced, you can store the last result it produced and check if the new is the same. If it is, generate another one. Taking the example from ATP's answer and modifying it to accomplish this, it would look as follows:
If you don't want it to ever repeat any result, and generate only as many results as possible values you have, then ATP's answer will do the trick.