此代碼應該將「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");
}
}
使用調試器,並找到自己 – redFIVE
好了,'while'循環任何無所事事,和'StringBuilder'用於反轉*整個*字符串,所以你期望什麼? – Andreas
這兩個有效點。直到今天早上,我不知道如何使用調試器。我一定會在下一次實施。 –