【发布时间】:2016-01-26 19:42:33
【问题描述】:
此代码应将我输入的“123ab 445 Hello”反转为“ba321 544 olleh”,但是,我得到“olleh 544 ba321”作为我的输出。为什么会这样?
import java.util.StringTokenizer;
import java.util.Scanner;
public class LessNaiveEncryption {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Provide an input sentence: ");
String userInput = keyboard.nextLine();
StringTokenizer strTokenizer = new StringTokenizer(userInput, " ", true);
System.out.print("The output sentence is : ");
while (strTokenizer.hasMoreTokens()) {
strTokenizer.nextToken();
}
StringBuilder blob = new StringBuilder(userInput);
blob.reverse();
System.out.println(blob);
System.out.print("\n");
}
}
【问题讨论】:
-
使用调试器自己找出答案
-
好吧,
while循环什么都不做,StringBuilder用于反转 整个 字符串,那么您期望什么? -
两个有效点。直到今天早上我才知道如何使用调试器。下次我一定会实施的。
标签: java tokenize stringbuilder