当我点击 n 或 N 时程序没有退出?
Program doesn't exit when I hit n or N?
我有以下作业问题:
Q1. Use nested for loops statements to draw empty boxes of any character (input from user). The boxes have the same number of rows and columns (input from the user; valid range: 5 to 21). Test for errors in input (including type)
SAMPLE OUTPUT:
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");
}
最简单的方法是:
检查循环内的输入 "N",然后跳出 while 循环,可能像这样:
if ((answer == 'n') || (answer == 'N')) {
break;
}
此外,您正在该程序中检查 y/n 输入 2 次。一个更好的写法是使用普通的 while 循环而不是 do-while 循环;显然,在这个问题中,如果您在一开始就输入 N,那么您根本不应该 运行ning 通过该程序。 Do-While 循环可用于确保程序 运行 至少执行一次(这不是此处应发生的情况;如果输入有效,程序应仅 运行 例如:"y").虽然使用 DW 循环是 "ok",但 while 循环在这里会更好。
你的循环可以这样写:
// you need this line to initially print the y/n question.
System.out.println("DO YOU WANT TO START Y OR N?");
// get an input and check if it is not n
while (Character.toUpperCase(input.next().charAt(0)) != 'N') {
// do stuff here
System.out.println("DO YOU WANT TO CONTINUE ? Y OR N"); // ask for next input
}
您需要在 Do you want to start(Y/N):
和 Do you want to continue(Y/N):
之后添加 N
的条件
System.exit(0)
用于终止程序。
输入这段代码
System.out.println("DO YOU WANT TO START Y OR N?");
answer = input.next().charAt(0);
if(answer == n || answer == N){
System.exit(0);
}
这是 Do you want to continue(Y/N):
System.out.println("DO YOU WANT TO CONTINUE ? Y OR N");
answer = input.next().charAt(0);
if(answer == n || answer == N){
System.exit(0);
}
编辑
如果你想打印'Game Stop'如果答案是N
,那么在System.exit(0)
之前使用Thread.sleep(timeInMilliseconds);
if(answer == n || answer == N){
Thread.sleep(5000); //This will make console wait for 5 seconds before exiting.
System.out.println("Game Stop."); //game stop will be printed for 5 seconds
System.exit(0);
}
我有以下作业问题:
Q1. Use nested for loops statements to draw empty boxes of any character (input from user). The boxes have the same number of rows and columns (input from the user; valid range: 5 to 21). Test for errors in input (including type)
SAMPLE OUTPUT:
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");
}
最简单的方法是:
检查循环内的输入 "N",然后跳出 while 循环,可能像这样:
if ((answer == 'n') || (answer == 'N')) {
break;
}
此外,您正在该程序中检查 y/n 输入 2 次。一个更好的写法是使用普通的 while 循环而不是 do-while 循环;显然,在这个问题中,如果您在一开始就输入 N,那么您根本不应该 运行ning 通过该程序。 Do-While 循环可用于确保程序 运行 至少执行一次(这不是此处应发生的情况;如果输入有效,程序应仅 运行 例如:"y").虽然使用 DW 循环是 "ok",但 while 循环在这里会更好。
你的循环可以这样写:
// you need this line to initially print the y/n question.
System.out.println("DO YOU WANT TO START Y OR N?");
// get an input and check if it is not n
while (Character.toUpperCase(input.next().charAt(0)) != 'N') {
// do stuff here
System.out.println("DO YOU WANT TO CONTINUE ? Y OR N"); // ask for next input
}
您需要在 Do you want to start(Y/N):
和 Do you want to continue(Y/N):
N
的条件
System.exit(0)
用于终止程序。
输入这段代码
System.out.println("DO YOU WANT TO START Y OR N?");
answer = input.next().charAt(0);
if(answer == n || answer == N){
System.exit(0);
}
这是 Do you want to continue(Y/N):
System.out.println("DO YOU WANT TO CONTINUE ? Y OR N");
answer = input.next().charAt(0);
if(answer == n || answer == N){
System.exit(0);
}
编辑
如果你想打印'Game Stop'如果答案是N
,那么在System.exit(0)
Thread.sleep(timeInMilliseconds);
if(answer == n || answer == N){
Thread.sleep(5000); //This will make console wait for 5 seconds before exiting.
System.out.println("Game Stop."); //game stop will be printed for 5 seconds
System.exit(0);
}