Why do i get infinite loop

谁说胖子不能爱 提交于 2019-12-12 21:26:37

问题


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();
            }
    }

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


回答1:


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);
    }
}

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



来源:https://stackoverflow.com/questions/28575627/why-do-i-get-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!