为我的JAVA算命程序生成非重复算命?

为我的JAVA算命程序生成非重复算命?,java,swing,Java,Swing,//在panel3中,我想将财富输出到JTextArea,我不想重复相同的财富两次。我在谷歌上试过一些东西,但都没用。 } //quitter quit=new JButton("Quit"); //Text inside the button quit.setFont(new Font("Times New Roman", Font.BOLD, 20)); quit.addActionListener((ActionEvent a

//在panel3中,我想将财富输出到JTextArea,我不想重复相同的财富两次。我在谷歌上试过一些东西,但都没用。 }

        //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);
首先使用将列表随机化

然后单击按钮时,您将从列表中删除第一个元素(
list#remove(int)
),直到列表中没有更多元素为止

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

从概念上讲,你想要的想法是

     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);
    }
}
import java.util.ArrayList;
导入java.util.Collections;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
private ArrayList fortuneDB=new ArrayList();
公开考试(){
字符串last=null;
对于(int-index=0;index<10;index++){
//这确保了最后一笔财富不会被浪费
//下一个周期的第一个。。。
做{
发财();
}while(fortuneDB.get(0).equals(last));
而(fortuneDB.size()>0){
last=db.remove(0);
系统输出打印项次(最后一次);
}
System.out.println(“--------------”;
}
}
受保护的void makeFortunes(){
fortuneDB=新的ArrayList();
fortuneDB.add(“你会得到一个好的GPA”);
fortuneDB.add(“你的平均成绩没有任何意义!”);
fortuneDB.add(“请关注CPII实验室”);
fortuneDB.add(“不要为了分数而交作业,确保你获得了知识”);
fortuneDB.add(“你会得到一个很好的合作”);
fortuneDB.add(“单击生成财富以发现您的真实财富”);
《财富》杂志(fortuneDB.add)(“天气很快就会好起来”);
fortuneDB.add(“买一个强力球”);
添加(“这是你的幸运数字17-19-23-50-51”);
财富数据库添加(“如果你在学校的力量球比赛中获胜”);
《财富》数据库添加(“即将到来的雪天”);
添加(“不要浪费你的时间”);
收藏。洗牌(财富数据库);
}
}

上面的帖子包含2个独立的.java文件1)算命框架2)算命查看器我无法在我的JTextArea(屏幕)上写一个生成新算命的循环,该区域在每次点击“吐算命”按钮时都会生成一个唯一的算命。注意:一旦生成了一个财富,它就不能在同一行中重复。可能的副本@ SuMthM:还考虑将<代码>列表<代码>作为S循环队列,如所引用的示例所示。@ MyDealthor感谢后续。我将在何处包含新的代码行,这些代码将随机化我的ArrayList中的项目,在您需要它们的时候,这取决于你是想要一个临时列表还是使用主列表,或者你是想在得到下一个报价之前随机化列表,或者只是随机化一下。我很抱歉,我对编程非常陌生,我很难理解。你能告诉我如何用我在网上找到的评论中的代码在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);
    }
}
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);
    }

}