程序结束时出现循环问题,无法退出
loop issue at end of program, can't exit
好的,谢谢,我已经找到退出循环了。
现在我的问题是我正在尝试测试 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);
这里是原代码....
/**
* 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
经典错误:
while (redo = true)
将true
分配给redo
并且总是true
,它不会测试它。
将其更改为
while (redo == true)
或更好:
while (redo)
其他循环条件也是如此。
好的,谢谢,我已经找到退出循环了。
现在我的问题是我正在尝试测试 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);
这里是原代码....
/**
* 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
经典错误:
while (redo = true)
将true
分配给redo
并且总是true
,它不会测试它。
将其更改为
while (redo == true)
或更好:
while (redo)
其他循环条件也是如此。