问题
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