//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;


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

        //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);


//在panel3中,我想向JTextArea输出财富,并且我不想重复两次相同的财富。我在Google上尝试了一些东西,但没有任何帮助。
        }

     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))删除,直到列表中没有其他元素。

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

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

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);
    }

}

09-28 06:02