Я новичок в Java и не могу найти ответ на мой вопрос. Я не уверен, правильно ли я устанавливаю циклы. "While loop" внутри "do while" должен выполняться только тогда, когда человек не вводит "Да" или "Нет" в следующий вопрос:
input = JOptionPane.showInputDialog("Do you have more grades to input? Please type 'Yes' or '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")) ) {
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
не знаю, почему вы написали слишком много строк :), спасибо :), комментарий, если какая-либо ошибка, я не проверял и не писал программу в java за последние 2 года: p, а также добавил объект вроде, вы продвигаете bel ike, Enter grande для ученика 1,2..n и т.д., вы также можете удалить это, если не хотите :)
Вы должны перейти на &&
из ||
, т.е. вы должны повторять, если вход отличается от "Да" и отличается от "Нет",
Вы можете переписать свой код так:
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'.");
}
Вы ошибаетесь
( (!input.equals("Yes")) || (!input.equals("No")) )
Когда ввод "Да", это оценивается как:
! true || ! false
что верно, поэтому цикл продолжается!
То, что вы хотите, вероятно,
while ( (! input.equals("Yes")) && (! input.equals("No")) )