简体   繁体   English

Java - 混淆字母

[英]Java - Mix up letters

Can someone give me an example of how to split Strings before you scramble the letters有人可以给我一个例子,说明如何在打乱字母之前拆分字符串

I can scramble the words but it changes the length of the words too我可以打乱单词,但它也会改变单词的长度

Example:例子:

input : Hello my name is Jon输入:你好,我叫乔恩

output: e imanoJs my nlolHe输出:e imanoJs my nlolHe

But it should be like this但它应该是这样的

input : Hello my name is Jon输入:你好,我叫乔恩

output: Hlelo my nmae is Jon输出:Hlelo 我的 nmae 是 Jon

so the first and last letter should stay in place所以第一个和最后一个字母应该保持原样

here is my code so far到目前为止,这是我的代码

public class MixUp{
    public static void main(String[] args){
        String cards="Hello my Name is Jon, nice to meet you";
        System.out.println("Input String = " + cards);
        cards = shuffle(cards);
        System.out.println("Shuffled String = " + cards);
     }

    static String shuffle(String cards){
        if (cards.length()<=1)
            return cards;

        int split=cards.length()/2;

        String temp1=shuffle(cards.substring(0,split));
        String temp2=shuffle(cards.substring(split));

        if (Math.random() > 0.5) 
            return temp1 + temp2;
        else
            return temp2 + temp1;
    }
}

inputString.split(" ") will split on spaces and return an array of Strings. inputString.split(" ")将在空格上拆分并返回一个字符串数组。 Create a new array, iterate through the first split array and shuffle each string and add the shuffled string to the new array.创建一个新数组,遍历第一个拆分数组并打乱每个字符串并将打乱的字符串添加到新数组中。

String cards="Hello my Name is Jon, nice to meet you";
System.out.println("Input String = " + cards);
String[] splt = cards.split(" ");
String[] shuffled = new String[splt.length];
for (int iter = 0; iter < splt.length; iter ++){
    shuffled[iter] = shuffle(splt[iter]);
}
// Now join the array

EDIT Better yet use a StringBuilder编辑最好还是使用 StringBuilder

String cards="Hello my Name is Jon, nice to meet you";
System.out.println("Input String = " + cards);
String[] splt = cards.split(" ");
StringBuilder sb = new StringBuilder();
for (int iter = 0; iter < shuffled.length; iter ++){
    sb.append(shuffle(splt[iter]) + " ");
}
String shuffled = sb.toString();

Notes笔记

  • Use Collections.shuffle() in combination with List.subList() so that the first and last letters are not moved.将 Collections.shuffle() 与 List.subList() 结合使用,以便不移动第一个和最后一个字母。
  • Convert to and from primitive array so that Collections.shuffle() can be used与原始数组相互转换,以便可以使用 Collections.shuffle()

Code代码

private static String shuffle(String sentence) {
    String[] words = sentence.split("\\s+");
    StringBuilder builder = new StringBuilder();
    for (String word : words) {
        List<Character> letters = new ArrayList<Character>();
        for (char letter : word.toCharArray()) {
            letters.add(letter);
        }
        if (letters.size() > 2) {
            Collections.shuffle(letters.subList(1, letters.size() - 1));
        }
        for (char letter : letters) {
            builder.append(letter);
        }
        builder.append(" ");
    }
    return builder.toString();
}

You should split the sentence into words and then scramble the words:您应该将句子拆分为单词,然后将单词打乱:

String[] words = sentence.split(" ");

for(String word : words)
   word = shuffle(word);

Then concat the word together to a sentence.然后将单词连接成一个句子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM