gpt4 book ai didi

java - 为我的JAVA算命先生程序生成不重复的算命?

转载 作者:行者123 更新时间:2023-11-30 07:33:58 33 4
gpt4 key购买 nike

//FortuneTellerFrame.java

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionEvent;
import java.util.Random;

public class FortuneTellerFrame extends JFrame
{
JPanel master, panel1, panel2, panel3;
JLabel image, heading, textArea;
ImageIcon logo;
JTextArea screen;
JScrollPane scroller;
JButton quit, generate;
ActionListener clicker, quiter;
ArrayList<String>fortuneDB=new ArrayList<>();

public FortuneTellerFrame()
{
super("Fortune Spitter");
master = new JPanel();
panel1();
panel2();
panel3();
fortuneHolder();

//add panels to master
master.setLayout(new BorderLayout());
master.add(panel1, BorderLayout.NORTH);
master.add(panel3, BorderLayout.SOUTH);
master.add(scroller, BorderLayout.CENTER);

//master adjustments
add(master);
setSize(750,750);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();

}

public void panel1()
{
panel1=new JPanel();
heading = new JLabel("Harry Potter Fortune Teller");
heading.setFont(new Font("Courier New", Font.ITALIC, 25));
logo=new ImageIcon("\\Users\\x\\Desktop\\harry.jpg");
image=new JLabel(logo);
panel1.add(image);
panel1.add(heading);
}

public void panel2()
{
panel2=new JPanel();
screen=new JTextArea(50,50);
screen.setEditable(false);
scroller=new JScrollPane(screen);
scroller.setVisible(true);
}

public void panel3()
{
panel3=new JPanel();
//fortune-spitter
generate = new JButton("Spit Fortune");
generate.setFont(new Font("Times New Roman", Font.BOLD, 20));
generate.addActionListener((ActionEvent ae) ->
//generate fortune button logic
//Please suggest a way in which I can generate a new fortune each time without having to repeat the old fortune again.
{
int currentRnd;
int previousRnd=0;
Random rnd = new Random();
do
{
currentRnd=rnd.nextInt(12);//there are 12 fortunes
}
while(currentRnd== previousRnd);
String fortune = fortuneDB.get(currentRnd);
int spot =screen.getCaretPosition();
screen.insert(fortune+"\n", spot);
currentRnd = previousRnd;

//我无法编写一个每次点击生成时都会生成新财富的循环。 }); panel3.add(生成);

        //quitter
quit=new JButton("Quit"); //Text inside the button
quit.setFont(new Font("Times New Roman", Font.BOLD, 20));
quit.addActionListener((ActionEvent ae) ->
{
System.exit(0);
});
panel3.add(quit);

//在面板3中,我想将命运输出到JTextArea,并且我不想重复相同的命运两次。我在谷歌上尝试了一些东西,但没有任何帮助。 }

     private void fortuneHolder() 
{
fortuneDB.add("You will get a good GPA");
fortuneDB.add("Your GPA does not mean anything!");
fortuneDB.add("Please catch up on CPII labs");
fortuneDB.add("Don't turn assignments in for points, make sure you gain the knowledge");
fortuneDB.add("You will get a good co-op");
fortuneDB.add("Click generate fortune to discover your true fortune");
fortuneDB.add("The weather will soon be nice");
fortuneDB.add("Buy a PowerBall");
fortuneDB.add("Here are your lucky numbers 17-19-23-50-51");
fortuneDB.add("If you win the Powerball dropuut of school");
fortuneDB.add("Snow Day Coming");
fortuneDB.add("Do Not waste your time");
}
}//end of FrameClass

//FortuneTellerViewer.java

import javax.swing.JFrame;

public class FortuneTellerViewer
{
public static void main(String[]args)
{
JFrame frame = new FortuneTellerFrame();
frame.setVisible(true);
}
}

最佳答案

首先使用Collections.shuffle随机化列表

然后,当单击该按钮时,您将从列表中删除第一个元素 (List#remove(int)),直到列表中不再有元素为止。

如果您需要原始列表,您可以使用辅助列表,这样,一旦用完,您就可以重新生成“选择列表”

从概念上讲,您所追求的想法类似于......

import java.util.ArrayList;
import java.util.Collections;

public class Test {

public static void main(String[] args) {
new Test();
}

private ArrayList<String> fortuneDB = new ArrayList<>();

public Test() {
String last = null;
for (int index = 0; index < 10; index++) {
// This makes sure that the last fortune isn't
// the first on the next cycle...
do {
makeFortunes();
} while (fortuneDB.get(0).equals(last));
while (fortuneDB.size() > 0) {
last = fortuneDB.remove(0);
System.out.println(last);
}
System.out.println("----------");
}
}

protected void makeFortunes() {
fortuneDB = new ArrayList<>();
fortuneDB.add("You will get a good GPA");
fortuneDB.add("Your GPA does not mean anything!");
fortuneDB.add("Please catch up on CPII labs");
fortuneDB.add("Don't turn assignments in for points, make sure you gain the knowledge");
fortuneDB.add("You will get a good co-op");
fortuneDB.add("Click generate fortune to discover your true fortune");
fortuneDB.add("The weather will soon be nice");
fortuneDB.add("Buy a PowerBall");
fortuneDB.add("Here are your lucky numbers 17-19-23-50-51");
fortuneDB.add("If you win the Powerball dropuut of school");
fortuneDB.add("Snow Day Coming");
fortuneDB.add("Do Not waste your time");
Collections.shuffle(fortuneDB);
}

}

关于java - 为我的JAVA算命先生程序生成不重复的算命?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35677323/

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