能有人給我如何分割字符串的例子你爭我奪的字母爪哇 - 混淆的字母
我能搶的話,但它改變的話長度之前太
例子:
輸入:你好我的名字是喬恩
輸出:電子imanoJs我nlolHe
但它應該是這樣的
輸入:你好我的名字是喬恩
輸出:Hlelo我nmae是喬恩
所以第一個和最後一個字母應該留在原地
這裏是我的代碼到目前爲止
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;
}
}
也許使用Arrays.asList(word.toCharArray())比使用for循環好。你怎麼看? – a11n 2014-12-02 16:47:56
Arrays.asList(word.toCharArray())生成一個列表,其中一個元素是數組。 Arrays.asList()僅適用於像Character這樣的包裝類型,而不是char。 – Adam 2014-12-02 16:51:52