本文介绍了为什么会出现无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Spreadsheet {

    public static final int row = 10;
    public static final int column = 7;
    static Cell[][] data;

    public static void print() {
        data = new Cell[column][row];
        for(int i = 0; i < row; i++) {
                for(int j = 0; j < column; j++) {
                    System.out.printf("%13s","|");
                }
                    System.out.println();
                for(int j = 0; j < column; j++) {
                    for(int k = 0; k < 12; k++) {
                        System.out.print("_");
                    }
                        System.out.print("+");
                }
                    System.out.println();
            }
    }

这应该会打印出一个10x7的电子表格,但不会这样做.在这里似乎是一个无限循环错误.帮助吗?

this should print out a 10x7 spreadsheet but it doesn't do that. seems like an infinite loop error here. help?

推荐答案

您只请求一次用户输入,然后继续重复使用相同的输入.您可以进行以下修复:

You only request user input once, and then keep re-using the same input. You can fix as follows:

while(input.hasNext()) {
    String userInput = input.nextLine();

    if (!userInput.equalsIgnoreCase("exit")) {
        commandLoop(userInput);
    }
}

您还可以在while条件中填充条件的赋值和评估,但我发现它的可读性较差.

You can also cram the assignment and evaluation of the condition in the while condition, but I find it to be less readable.

这篇关于为什么会出现无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:33