【问题标题】:Why do i get infinite loop为什么我会无限循环
【发布时间】:2015-02-18 03:47:25
【问题描述】:
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 的电子表格,但它没有这样做。这里似乎是一个无限循环错误。帮忙?

【问题讨论】:

  • 什么时候重新提示用户输入更多命令?
  • 如果你在循环外请求 nextLine 你永远不会得到下一行。

标签: java infinite-loop


【解决方案1】:

您只请求用户输入一次,然后继续重复使用相同的输入。您可以按以下方式修复:

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

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

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

【讨论】:

  • @MadProgrammer 谢谢。还没有完全清醒:-)
  • 嘿嘿嘿,懂那种感觉;)
猜你喜欢
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 2011-04-30
  • 2011-04-19
  • 2011-08-18
相关资源
最近更新 更多