我是Java语言的初学者,最近发现了这个关于缓冲字符串的字符串程序。我在 IDE 中输入了代码,但是不断弹出语法错误,导致无法编译代码。该错误似乎与我最后一个 for 循环中的 count7lt 变量有关。
这是代码:
public class StringBuffer{
public static void main(String[] args) {
countTo_N_Improved();
}
private final static int MAX_LENGTH=30;
private static String buffer = "";
private static void emit(String nextChunk) {
if(buffer.length() + nextChunk.length() > MAX_LENGTH) {
System.out.println(buffer);
buffer = "";
}
buffer += nextChunk;
}
private static final int N=100;
private static void countTo_N_Improved() {
for (int count=2; count7lt;=N; count=count+2) {
emit(" " + count);
}
}
}
最佳答案
你的最后一个 for 循环有太多分号。
我的猜测是您正在复制代码,这是可以的 - 但如果您仔细观察,您会发现它不符合 for(;;) 循环的标准格式,并且它包含一个您没有的变量t 创建。
Java 非常擅长及早发现问题并告诉您问题所在。阅读并考虑错误很重要。
关于java - 当我尝试缓冲字符串时,我的代码中哪里搞砸了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37575184/