【问题标题】:Shuffling string word by word逐字洗牌
【发布时间】:2016-05-28 14:20:55
【问题描述】:

美好的一天。我们有一个任务,要求用户在文本区域输入一些内容,当他点击确定按钮时,弹出框应该让他输入的内容混乱。 (例如:“Hello World Tuna”变成“olHel odlWr Tnau”)。我设法为洗牌做了一个代码,但它把所有的词都弄乱了,而不是一个字一个字。

我们不允许使用集合或任何东西,那么有没有办法不使用它们呢?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class ChallengeSwapper2 extends JFrame
{
JTextArea txtArea;
JScrollPane scrPane;
JLabel lbl;
JButton btn;
Container con;

public ChallengeSwapper2()
{
    super("Challenge Swapper!");
    setLayout(new FlowLayout());
    con = getContentPane();

    lbl = new JLabel("INPUT");
    lbl.setFont(new Font("Arial", Font.BOLD, 20));
    con.add(lbl);

    txtArea = new JTextArea(10,15);
    txtArea.setWrapStyleWord(true);
    txtArea.setLineWrap(true);
    txtArea.setFont(new Font("Verdana", Font.BOLD, 20));

    scrPane = new JScrollPane(txtArea);
    scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    con.add(scrPane);

    btn = new JButton("OK");
    btn.setFont(new Font("Arial", Font.BOLD, 10));
    btn.setBounds(10,10, 10, 10);
    btn.addActionListener(new btnFnc());
    con.add(btn);
}

public class btnFnc implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {

        if (txtArea.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null,"Please enter something tanga", "Error Message",JOptionPane.INFORMATION_MESSAGE);
        }
        else 
        {
            String sentence = txtArea.getText();
            String[] words = sentence.split(" ");
            char [] letters = sentence.toCharArray();

            for( int i=0 ; i<words.length-1 ; i++ )
            {
                int j = (char)(Math.random() * letters.length);
                char temp = letters[i]; 
                letters[i] = letters[j];  
                letters[j] = temp;
            }

            String newtxt = new String(letters);

            JOptionPane.showMessageDialog(null, newtxt, "Swapper Challenge", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}


public static void main (String [] args)
{
    ChallengeSwapper2 gui = new ChallengeSwapper2();
    gui.setResizable(false);
    gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
    gui.setBounds(300,300,330,370);
    gui.show();
}
}

【问题讨论】:

    标签: java string shuffle


    【解决方案1】:

    您当前的代码将句子拆分为单词,但随后您还将句子转换为字符数组并操作整个句子。

    将句子拆分成单词后,将每个单词转换为字符数组,并将每个单词中的字母打乱,然后重构句子。

    我鼓励您将改组转移到一种新方法中,这样您就可以隔离改组过程以提高清晰度和简化测试。

    【讨论】:

      【解决方案2】:

      试试这个。

          String s = "Hello World Tuna";
          Random r = new Random();
          String result = Stream.of(s.split(" "))
              .map(x -> x.chars()
                  .mapToObj(c -> "" + (char)c)
                  .reduce("", (a, c) -> {
                      int i = r.nextInt(a.length() + 1);
                      return a.substring(0, i) + c + a.substring(i);
                  }))
              .collect(Collectors.joining(" "));
          System.out.println(result);
      

      【讨论】:

        猜你喜欢
        • 2021-10-05
        • 1970-01-01
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多