下面的这个程序在运行时不会停止。我不认为我犯了重大错误,但请帮助我,因为我是 Java 新手。
import java.util.*;
public class ReverseWords {
public static void main( String args[] ) {
String paragraph;
Scanner input = new Scanner (System.in);
System.out.print("Enter a paragraph: ");
paragraph = input.nextLine();
paragraph = paragraph.trim();
StringTokenizer tokens = new StringTokenizer(paragraph, ".");
while (tokens.hasMoreTokens()){
for (int i = paragraph.length() - 1; i>=0; i--) {
System.out.print(paragraph.charAt(i));
}
System.out.print(". ");
}
System.out.println();
}
}
当我输入时: The quick brown fox. Jumps over. The lazy dog.
它输出: .god yzal ehT .revo spmuJ .xof nworb kciuq ehT.
无限次。
非常感谢!