根据您的问题,我会用一个StringBuilder
。因为,当索引是偶数时,它有一个有用的reverse()
函数。我相信你想要类似
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String line = sc.nextLine();
// The \\s+ pattern matches one or more white-space characters.
String[] words = line.split("\\s+");
for (int i = 0; i < words.length; i++) {
if (i != 0) {
System.out.print(' ');
}
StringBuilder sb = new StringBuilder(words[i]);
// Reverse every other word.
if (i % 2 == 0) {
sb.reverse();
}
System.out.print(sb);
}
System.out.println();
}
}
这是怎么不问问题! – Andy 2014-11-05 01:26:39
请正确格式化您的问题并显示您尝试过的不工作 – mdewitt 2014-11-05 01:27:58