数组中的随机字符串,不能连续选择两个相同的字符串 Java



我有一个问题,弄清楚从我的数组中加入jtextarea的随机字符串元素的最佳方法可能是。我的问题是,我无法连续输出我的数组输出中的两个字符串。(不能与最后一个提出的相同)。我尝试过循环时尝试过循环。我只是不太确定如何编写此约束。任何朝正确方向的指针都将不胜感激。在这个问题上已经有一段时间了,我已经从上到下拖了stackoverlow。我的代码现在运行正常,但是我遇到的问题是在BTNSubmit ActionListener中的CreateBottompanel()方法中。谢谢

package Tell;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.Random;
public class FortuneTellerFrame extends JFrame {
JPanel pnlTop, pnlMiddle, pnlBottom;
JButton btnSubmit, btnQuit;
JLabel  lblFortuneTeller, lblPassword;
JTextArea txaResults;
JScrollPane jsp;
String[] fortunes = {"A loved one will die","You will recieve a gift",
    "A mysterious person will come into your life","You will encounter misfortune soon",
    "Life will become easier for you","Sadness will overcome you",
    "Good tidings are in your future","Some sum of money will find its way to you",
    "Good news is around the corner","Persevere and you shall be rewarded",
    "You will run out of time at a bad moment","You will fall and break a leg"};
public FortuneTellerFrame() {
    add(this.createTopPanel(), BorderLayout.NORTH);
    add(this.createMiddlePanel(), BorderLayout.CENTER);
    add(this.createBottomPanel(), BorderLayout.SOUTH);
    // Always set the size of data and the default close operation. 
    // Visibility needs to be set to true to be seen as well
    this.setSize(400, 300);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }
   private JPanel createTopPanel()
   {
       pnlTop = new JPanel();
       pnlTop.setLayout(new GridLayout(2,2));
       ImageIcon icon = new ImageIcon("ball.jpg");
       Image image1 = icon.getImage(); // transform it 
       Image newimg = image1.getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH); 
       icon = new ImageIcon(newimg); // transform back
       JLabel label = new JLabel("Fortune Teller",icon,JLabel.CENTER);
       label.setForeground(Color.red);
       label.setFont(new Font ("Lucida Sans Unicode", Font.BOLD, 20));
       label.setHorizontalTextPosition(JLabel.CENTER);
       label.setVerticalTextPosition(JLabel.BOTTOM);
       pnlTop.add(label);
       return pnlTop;
    }
   private JPanel createMiddlePanel()
   {
       pnlMiddle = new JPanel();
       txaResults = new JTextArea(10, 30);
       txaResults.setFont(new Font ("Serif", Font.ITALIC, 10));
       jsp = new JScrollPane(txaResults,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
               JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
       pnlMiddle.add(jsp);
       return pnlMiddle;
   }
   private JPanel createBottomPanel()
   {
       pnlBottom = new JPanel();
       btnSubmit = new JButton("Read My Fortune!");
       btnQuit = new JButton("Quit");
       btnSubmit.setFont(new Font ("Arial", Font.BOLD, 9));
       btnQuit.setFont(new Font ("Arial", Font.BOLD, 9));       
      btnQuit.addActionListener(e ->{
          System.exit(0);
      });
      btnSubmit.addActionListener(e ->{
          //String username = this.txtFortuneTeller.getText();
           Random rand = new Random();
           String x = fortunes[rand.nextInt(fortunes.length)]; 
          this.txaResults.append(x + "n");
      });
       pnlBottom.add(btnSubmit);
       pnlBottom.add(btnQuit);
       return pnlBottom;
   }

}

如果我理解正确并且每个答案可能多次发生,但直接连续的情况下,最简单的方法是记住最后的财富,如果发生两次,则跳过:

class FortuneAppenderAction implements ActionListener {
    private String lastFortune = null;
    @Override
    public void actionPerformed( ActionEvent e ) {
        Random rand = new Random();
        String x;
        do {
            x = fortunes[rand.nextInt( fortunes.length )];
        }
        while ( x.equals( lastFortune ) );
        lastFortune = x;
        txaResults.append( x + "n" );
    }
}

createBottomPanel()

btnSubmit.addActionListener( new FortuneAppenderAction() );

最新更新