Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-混合字母_Java_String_Split_Shuffle - Fatal编程技术网

Java-混合字母

Java-混合字母,java,string,split,shuffle,Java,String,Split,Shuffle,有人能给我举个例子,在你把字母拼凑起来之前,如何把字符串分开吗 我可以拼凑单词,但它也会改变单词的长度 例如: 输入:你好,我叫乔恩 输出:e imanoJs my nlolHe 但应该是这样的 输入:你好,我叫乔恩 输出:Hlelo我的nmae是Jon 因此,第一个和最后一个字母应该保持不变 这是到目前为止我的代码 public class MixUp{ public static void main(String[] args){ String cards="Hell

有人能给我举个例子,在你把字母拼凑起来之前,如何把字符串分开吗

我可以拼凑单词,但它也会改变单词的长度

例如:

输入:你好,我叫乔恩

输出:e imanoJs my nlolHe

但应该是这样的

输入:你好,我叫乔恩

输出:Hlelo我的nmae是Jon

因此,第一个和最后一个字母应该保持不变

这是到目前为止我的代码

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;
    }
}
公共类混淆{
公共静态void main(字符串[]args){
String cards=“你好,我叫乔恩,很高兴认识你”;
System.out.println(“输入字符串=“+卡”);
牌=洗牌(牌);
System.out.println(“无序字符串=“+卡”);
}
静态串洗牌(串牌){
如果(卡片长度()0.5)
返回temp1+temp2;
其他的
返回temp2+temp1;
}
}
inputString.split(“”)
将在空格上拆分并返回字符串数组。创建一个新数组,遍历第一个拆分数组并洗牌每个字符串,然后将洗牌后的字符串添加到新数组中

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
String cards=“你好,我叫乔恩,很高兴认识你”;
System.out.println(“输入字符串=“+卡”);
字符串[]splt=cards.split(“”);
String[]shuffled=新字符串[splt.length];
对于(int-iter=0;iter
编辑最好使用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();
String cards=“你好,我叫乔恩,很高兴认识你”;
System.out.println(“输入字符串=“+卡”);
字符串[]splt=cards.split(“”);
StringBuilder sb=新的StringBuilder();
for(int-iter=0;iter
你应该把句子分成几个单词,然后把单词拼凑起来:

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

for(String word : words)
   word = shuffle(word);
然后把单词连在一起拼成一个句子。

注释

  • 将Collections.shuffle()与List.subList()结合使用,以便不移动第一个和最后一个字母
  • 在基元数组之间进行转换,以便可以使用Collections.shuffle()
代码

私有静态字符串洗牌(字符串语句){
字符串[]单词=句子。拆分(\\s+);
StringBuilder=新的StringBuilder();
for(字符串字:字){
列表字母=新的ArrayList();
for(字符字母:word.toCharArray()){
字母。添加(字母);
}
如果(字母.size()>2){
Collections.shuffle(letters.subList(1,letters.size()-1));
}
用于(字符:字母){
附(信);
}
生成器。追加(“”);
}
返回builder.toString();
}

修改word不起任何作用,您必须连接到在for循环之外声明的字符串。可能使用array.asList(word.tocharray())比使用for循环更好。你认为呢?Arrays.asList(word.toCharArray())生成一个包含一个元素的列表,这个元素就是数组。asList()仅适用于诸如Character之类的包装类型,而不适用于char。
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();
}