好的,谢谢,我已经找到了退出循环。
现在我的问题是我试图测试int newScore以确保它的值在1到100之间。循环中途工作。如果我在参数之外输入一个值,它会循环并再次询问它。但是,当我在参数中输入一个值时,它不会打破循环并继续到程序的下一部分(要么提出下一个测试分数,要么计算平均值,如果没有更多分数)这是我更新的代码尝试这个循环:
do {
while (scoreValid) {
while (tv) {
try {
System.out.println("Please enter test score # " + counter);
newScore = Integer.parseInt(scan.nextLine());
if (newScore >= 1 && newScore <= 100) {
scoreValid = true;
tv = false;
counter++;
totalScore = totalScore + newScore;
break;
} else {
System.out.println("Sorry, you must enter a number between 1 and 100.");
tv = true;
continue;
}
} catch (Exception e) {
System.out.println("Sorry, I didn't catch that");
continue;
}
}
}
} while (counter <= numberTests);
&#13;
这是原始代码......
/**
* This app will average test scores and tell you what letter grade you received.
*
* @jordan.hawkes
* @1.0
*/
import java.util.Scanner;
public class TestScoreCalculator {
public static void main(String[] args) {
String first; // First Name entered by user
String last; // Last Name entered by user
String newTestAnswer; // answer for entering another test score
String redoAnswer; // answer to rerun program
int avg; // Average test score
int totalScore = 0; // Total Running Test Score
int newScore; // Test Score Entered by User
int counter = 1; // Number of Scores Entered
int testNumber; // Test # counter
int numberTests = 0; // Number of Tests entered by user
boolean scoreValid = false; // boolean to validate score
boolean newTest = false; // boolean to validate entering another test
boolean redo = false; // boolean to rerun program
boolean numberTestsValid = false; // boolean to validate number of tests
boolean test = false; // test boolean to validate exit/rerun
Scanner scan = new Scanner(System.in);
System.out.print("Please enter your first name: ");
first = scan.nextLine();
System.out.print("Hello, " + first + ", please enter your last name: ");
last = scan.nextLine();
while (redo = true) {
while (numberTestsValid = true) {
try {
System.out.println("Thanks, " + first + ", how many test scores would you like to enter? ");
numberTests = Integer.parseInt(scan.nextLine());
numberTestsValid = true;
break;
} catch (Exception e) {
System.out.println("Sorry, I didn't catch that");
continue;
}
}
do {
while (scoreValid = true) {
try {
System.out.println("Great, please enter test score # " + counter);
newScore = Integer.parseInt(scan.nextLine());
scoreValid = true;
counter++;
totalScore = totalScore + newScore;
break;
} catch (Exception e) {
System.out.println("Sorry, I didn't catch that");
continue;
}
}
} while (counter <= numberTests);
testNumber = counter - 1;
avg = (totalScore / testNumber);
switch (avg / 10) {
case 10:
System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got an A+!");
break;
case 9:
System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got an A!");
break;
case 8:
System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got a B!");
break;
case 7:
System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got a C!");
break;
case 6:
System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got a D!");
break;
default:
System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got an F!");
}
while (test = true) {
System.out.println("type r to restart or q to quit");
redoAnswer = scan.nextLine().trim().toLowerCase();
if (redoAnswer.equals("r")) {
test = false;
totalScore = 0;
counter = 1;
redo = true;
break;
} else if (redoAnswer.equals("q")) {
test = false;
redo = false;
System.out.println("Goodbye!");
continue;
} else {
System.out.println("Sorry, I didn't catch that. Please type r to restart or q to quit");
continue;
}
}
}
} //end main
} //end class
&#13;
答案 0 :(得分:0)
经典错误:
while (redo = true)
将 true
分配给redo
并始终为true
,但不会对其进行测试。
将其更改为
while (redo == true)
或更好:
while (redo)
其他循环条件也一样。