在 do while 循环中嵌套 while 循环
Nest while loop inside do while loop
我是 Java 的新手,似乎找不到我的问题的答案。
我不确定我是否正确嵌套循环。
"do while"里面的"while loop"只有在没有输入"Yes"或者"No"的情况下才会执行下面的问题:
input = JOptionPane.showInputDialog("Do you have more grades to input? Please type 'Yes' or 'No'.");
但事实证明,如果我输入"Yes"或"No",循环仍在执行。
如果有人能指出我做错了什么,我将不胜感激! “
String input;
int grade;
do {
input = JOptionPane.showInputDialog("Enter the midterm grades, please");
grade = Integer.parseInt(input);
if ( grade < 0 || grade > 100 ) {
input = JOptionPane.showInputDialog("You have entered an invalid grade. The grade should be a number between 0 and 100. Enter the midterm grades, please.");
grade = Integer.parseInt(input);
}
input = JOptionPane.showInputDialog("Do you have more grades to input? Please type 'Yes' or 'No'.");
while ( (!input.equals("Yes")) || (!input.equals("No")) ) {
input = JOptionPane.showInputDialog("You should type 'Yes' if you want to input more grades or 'No' if you are done with inputing grades. Do you have more grades to input? Please type 'Yes' or 'No'.");
if ( (input.equals("Yes")) || (input.equals("No")) ) {
break;
} else {
continue;
}
}
} while ( input.equals("Yes") );
看起来像这样:
while ( (!input.equals("Yes")) || (!input.equals("No")) ) {
应该像这样改成&&
:
while ( (!input.equals("Yes")) && (!input.equals("No")) ) {
你的条件不对
( (!input.equals("Yes")) || (!input.equals("No")) )
当输入为 "Yes" 时,计算结果为:
! true || ! false
这是真的,所以循环继续!
你想要的大概是
while ( (! input.equals("Yes")) && (! input.equals("No")) )
您应该从 ||
更改为 &&
,即。如果输入与 "Yes" 和 不同 "No"
不同,则应进行迭代
您可以将代码重写为:
while (!input.equals("Yes") && !input.equals("No") ) {
input = JOptionPane.showInputDialog("You should type 'Yes' if you want to input more grades or 'No' if you are done with inputing grades. Do you have more grades to input? Please type 'Yes' or 'No'.");
}
String input = null;
int grade = 0; // local variables don't initlized by default value in function, only class fields
int counter = 0;
do {
counter++;
do{
grade = Integer.parseInt(JOptionPane.showInputDialog("Enter the midterm grades grade for " + counter + " student must be between 0-100 : , please"));
}while(grade < 0 || grade > 100); // promot user again if it enters data in wrong range till , he/she don't input correct value
input = JOptionPane.showInputDialog("If you want to Enter more grades then type 'Y' or 'Yes' .. ");
} while ( (input.toLowerCase()).equals("yes") || (input.toLowerCase()).equals("y")); // lower case because some users will type like "YEs" or something, can also use EqualsIgnoreCase function
不知道你为什么写了太多行 :),谢谢 :),如果有任何错误我没有检查并且在过去 2 年没有在 java 中编写程序:p,并且还添加了一个设施,比如,你晋升会喜欢,为学生 1,2..n 等输入 grande,如果你不想的话,你也可以删除它:)
我是 Java 的新手,似乎找不到我的问题的答案。 我不确定我是否正确嵌套循环。 "do while"里面的"while loop"只有在没有输入"Yes"或者"No"的情况下才会执行下面的问题:
input = JOptionPane.showInputDialog("Do you have more grades to input? Please type 'Yes' or 'No'.");
但事实证明,如果我输入"Yes"或"No",循环仍在执行。 如果有人能指出我做错了什么,我将不胜感激! “
String input;
int grade;
do {
input = JOptionPane.showInputDialog("Enter the midterm grades, please");
grade = Integer.parseInt(input);
if ( grade < 0 || grade > 100 ) {
input = JOptionPane.showInputDialog("You have entered an invalid grade. The grade should be a number between 0 and 100. Enter the midterm grades, please.");
grade = Integer.parseInt(input);
}
input = JOptionPane.showInputDialog("Do you have more grades to input? Please type 'Yes' or 'No'.");
while ( (!input.equals("Yes")) || (!input.equals("No")) ) {
input = JOptionPane.showInputDialog("You should type 'Yes' if you want to input more grades or 'No' if you are done with inputing grades. Do you have more grades to input? Please type 'Yes' or 'No'.");
if ( (input.equals("Yes")) || (input.equals("No")) ) {
break;
} else {
continue;
}
}
} while ( input.equals("Yes") );
看起来像这样:
while ( (!input.equals("Yes")) || (!input.equals("No")) ) {
应该像这样改成&&
:
while ( (!input.equals("Yes")) && (!input.equals("No")) ) {
你的条件不对
( (!input.equals("Yes")) || (!input.equals("No")) )
当输入为 "Yes" 时,计算结果为:
! true || ! false
这是真的,所以循环继续!
你想要的大概是
while ( (! input.equals("Yes")) && (! input.equals("No")) )
您应该从 ||
更改为 &&
,即。如果输入与 "Yes" 和 不同 "No"
您可以将代码重写为:
while (!input.equals("Yes") && !input.equals("No") ) {
input = JOptionPane.showInputDialog("You should type 'Yes' if you want to input more grades or 'No' if you are done with inputing grades. Do you have more grades to input? Please type 'Yes' or 'No'.");
}
String input = null;
int grade = 0; // local variables don't initlized by default value in function, only class fields
int counter = 0;
do {
counter++;
do{
grade = Integer.parseInt(JOptionPane.showInputDialog("Enter the midterm grades grade for " + counter + " student must be between 0-100 : , please"));
}while(grade < 0 || grade > 100); // promot user again if it enters data in wrong range till , he/she don't input correct value
input = JOptionPane.showInputDialog("If you want to Enter more grades then type 'Y' or 'Yes' .. ");
} while ( (input.toLowerCase()).equals("yes") || (input.toLowerCase()).equals("y")); // lower case because some users will type like "YEs" or something, can also use EqualsIgnoreCase function
不知道你为什么写了太多行 :),谢谢 :),如果有任何错误我没有检查并且在过去 2 年没有在 java 中编写程序:p,并且还添加了一个设施,比如,你晋升会喜欢,为学生 1,2..n 等输入 grande,如果你不想的话,你也可以删除它:)