【发布时间】:2014-12-02 16:16:49
【问题描述】:
在你打乱字母之前,谁能给我一个如何拆分字符串的例子
我可以打乱单词,但它也会改变单词的长度
例子:
输入:你好,我叫乔恩
输出:e imanoJs my nlolHe
但应该是这样的
输入:你好,我叫乔恩
输出:Hlelo my nmae is 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;
}
}
【问题讨论】: