所以我有这个循环,它将获取一行文本并将其设置为一个字符串以供方法处理。
while (input.hasNextLine()){
String text = input.nextLine();
processLine(text);
}
processLine 方法是。
public static void processLine(String text) {
Scanner data = new Scanner(text);
while (data.hasNext()){
String str = "hi";
int tabCount = 0;
str = data.next();
System.out.print(str + " ");
if (str.equals("{")) {
tabCount++;
System.out.println();
for (int i = 0; i < TAB_SIZE * tabCount; i++){
System.out.print(" ");
}
}
}
发生的事情是我的 tabCount 没有增加,但它仍在执行 println。怎么会这样?这是输出。
public class Test1 {
public static void main( String[] args ) {
System.out.println( "This is Test 1." ); } }
感谢任何帮助。
最佳答案
tabCount
变量在 while 循环
内,因此每次迭代都会重置,即它始终为 0
尝试
public static void processLine(String text) {
Scanner data = new Scanner(text);
int tabCount = 0;
while (data.hasNext()){
String str = "hi";
str = data.next();
System.out.print(str + " ");
if (str.equals("{")) {
tabCount++;
System.out.println();
for (int i = 0; i < TAB_SIZE * tabCount; i++){
System.out.print(" ");
}
}
}
关于 java 。文件处理。 For循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22087039/