我有以下作业问题:
Q1。使用嵌套的 for 循环语句来绘制任何字符的空框(来自用户的输入)。这些框的行数和列数相同(用户输入;有效范围:5 到 21)。测试输入错误(包括类型)
样品输出:
Do you want to start(Y/N): y How many chars/last row? n Not an integer! Try again! How many chars/last row? fgfgfg Not an integer! Try again! How many chars/last row? 7.6 Not an integer! Try again! How many chars/last row? 34 ERROR! Valid range 5 - 21. How many chars/last row? 7 What character? k Do you want to continue(Y/N): y
我已经编写了下面的代码,但是当我点击“n”或“N”时它并没有退出,我不知道为什么。我将如何解决这个问题?
public static void main(String[] args) {
Scanner input = new Scanner(System. in );
char answer = 'n';
int row = 0;
char output = 'k';
do {
System.out.println("DO YOU WANT TO START Y OR N?");
answer = input.next().charAt(0);
System.out.println("enter the number of rows");
while (!input.hasNextInt()) {
System.out.println("Not an integer,try again ");
input.next();
}
row = input.nextInt();
while (row < 5 || row > 21) {
System.out.println("ERROR! Valid range 5 - 21. How many chars/last row?");
row = input.nextInt();
}
System.out.println("WHAT CHARACTER?");
output = input.next().charAt(0);
for (int i = 0; i < row; i++) { //nested for loop to create the box
System.out.print(output);
}
System.out.println();
for (int i = 0; i < row - 2; i++) {
System.out.print(output);
for (int j = 0; j < row - 2; j++) {
System.out.print(" ");
}
System.out.print(output);
System.out.println();
}
for (int i = 0; i < row; i++) {
System.out.print(output);
}
System.out.println();
System.out.println();
System.out.println("DO YOU WANT TO CONTINUE ? Y OR N");
answer = input.next().charAt(0);
} while (answer == 'Y' || answer == 'y');
input.close();
System.out.println("game stop");
}