为什么这样做会无限循环运行?
Why is this do while loop running infinitely?
尝试编写一个程序,让用户输入他们本周的开支。
问题在于我希望程序在输入不可接受的输入时重新询问用户。我想出了如何检测负值,但是当试图捕获输入不匹配异常时(如果输入了字符或字符串)循环只是无限地运行询问 "Monday Expenses:"
我该怎么做才能让用户有另一次回答的机会?我试着休息一下;但这也脱离了 do while 循环,这是我不想要的。
到目前为止,这是我的代码:
import java.util.Scanner;
public class BarGraph{
public static void main (String[] args){
Scanner myScanner;
myScanner = new Scanner(System.in);
double mon, tues, wed, thurs, fri, sat, sun;
do{
try{
System.out.print("Expenses for Monday: ");
mon = myScanner.nextDouble();
}catch( Exception e){
mon = -1;
}
}while(mon<0);
System.out.println(mon);
}
}
感谢您的帮助
替换
mon = myScanner.nextDouble();
与以下
mon = Double.parseDouble(myScanner.next());
这是因为它总是在扫描器无法解析双精度值时转到 catch 块。
符合你的条件
mon<0
它总是得到-1值,它导致无限循环
你可能把你的条件误认为是
while(mon>0)
对 nextDouble()
的调用不会消耗来自 Scanner
的非 double
。你需要这样的东西,
if (myScanner.hasNextDouble()) {
mon = myScanner.nextDouble();
} else {
System.out.printf("%s is not a double.%n", myScanner.next());
}
此外,我建议您将该逻辑提取到一个方法中。喜欢,
static double getExpensesForDay(Scanner scanner, String day) {
while (true) {
System.out.printf("Expenses for %s: ", day);
System.out.flush();
if (scanner.hasNextDouble()) {
return scanner.nextDouble();
}
System.out.printf("%s is not a double.%n", scanner.nextLine());
}
}
然后你可以这样称呼它
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
double mon = getExpensesForDay(myScanner, "Monday");
double tues = getExpensesForDay(myScanner, "Tuesday");
double wed = getExpensesForDay(myScanner, "Wednesday");
double thurs = getExpensesForDay(myScanner, "Thursday");
double fri = getExpensesForDay(myScanner, "Friday");
double sat = getExpensesForDay(myScanner, "Saturday");
double sun = getExpensesForDay(myScanner, "Sunday");
// ...
}
你可以使用myScanner.nextLine();在开始下一个循环之前转储缓存中所有不需要的字符
import java.util.Scanner;
public class BarGraph {
public static void main(String[] args) {
Scanner myScanner;
myScanner = new Scanner(System.in);
double mon, tues, wed, thurs, fri, sat, sun;
do {
try {
System.out.print("Expenses for Monday: ");
mon = myScanner.nextDouble();
} catch (Exception e) {
mon = -1;
myScanner.nextLine();
}
} while (mon < 0);
System.out.println(mon);
}
}
您可以试试下面的代码。它将遍历整个数组。您可以根据需要添加更多验证。
import java.util.Scanner;
public class BarGraph
{
public static void main(String[] args)
{
System.out.println("Welcome to Expenses Program: " + "\n");
week();
}
public static void week()
{
Scanner myScanner;
myScanner = new Scanner(System.in);
String [] day = new String[] {"mon", "tues", "wed", "thurs", "fri", "sat", "sun"};
for (String days : day)
{
System.out.println("Expenses for " + days + ":");
float exp = myScanner.nextFloat();
}
}
}
尝试编写一个程序,让用户输入他们本周的开支。 问题在于我希望程序在输入不可接受的输入时重新询问用户。我想出了如何检测负值,但是当试图捕获输入不匹配异常时(如果输入了字符或字符串)循环只是无限地运行询问 "Monday Expenses:" 我该怎么做才能让用户有另一次回答的机会?我试着休息一下;但这也脱离了 do while 循环,这是我不想要的。
到目前为止,这是我的代码:
import java.util.Scanner;
public class BarGraph{
public static void main (String[] args){
Scanner myScanner;
myScanner = new Scanner(System.in);
double mon, tues, wed, thurs, fri, sat, sun;
do{
try{
System.out.print("Expenses for Monday: ");
mon = myScanner.nextDouble();
}catch( Exception e){
mon = -1;
}
}while(mon<0);
System.out.println(mon);
}
}
感谢您的帮助
替换
mon = myScanner.nextDouble();
与以下
mon = Double.parseDouble(myScanner.next());
这是因为它总是在扫描器无法解析双精度值时转到 catch 块。 符合你的条件
mon<0
它总是得到-1值,它导致无限循环 你可能把你的条件误认为是
while(mon>0)
对 nextDouble()
的调用不会消耗来自 Scanner
的非 double
。你需要这样的东西,
if (myScanner.hasNextDouble()) {
mon = myScanner.nextDouble();
} else {
System.out.printf("%s is not a double.%n", myScanner.next());
}
此外,我建议您将该逻辑提取到一个方法中。喜欢,
static double getExpensesForDay(Scanner scanner, String day) {
while (true) {
System.out.printf("Expenses for %s: ", day);
System.out.flush();
if (scanner.hasNextDouble()) {
return scanner.nextDouble();
}
System.out.printf("%s is not a double.%n", scanner.nextLine());
}
}
然后你可以这样称呼它
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
double mon = getExpensesForDay(myScanner, "Monday");
double tues = getExpensesForDay(myScanner, "Tuesday");
double wed = getExpensesForDay(myScanner, "Wednesday");
double thurs = getExpensesForDay(myScanner, "Thursday");
double fri = getExpensesForDay(myScanner, "Friday");
double sat = getExpensesForDay(myScanner, "Saturday");
double sun = getExpensesForDay(myScanner, "Sunday");
// ...
}
你可以使用myScanner.nextLine();在开始下一个循环之前转储缓存中所有不需要的字符
import java.util.Scanner;
public class BarGraph {
public static void main(String[] args) {
Scanner myScanner;
myScanner = new Scanner(System.in);
double mon, tues, wed, thurs, fri, sat, sun;
do {
try {
System.out.print("Expenses for Monday: ");
mon = myScanner.nextDouble();
} catch (Exception e) {
mon = -1;
myScanner.nextLine();
}
} while (mon < 0);
System.out.println(mon);
}
}
您可以试试下面的代码。它将遍历整个数组。您可以根据需要添加更多验证。
import java.util.Scanner;
public class BarGraph
{
public static void main(String[] args)
{
System.out.println("Welcome to Expenses Program: " + "\n");
week();
}
public static void week()
{
Scanner myScanner;
myScanner = new Scanner(System.in);
String [] day = new String[] {"mon", "tues", "wed", "thurs", "fri", "sat", "sun"};
for (String days : day)
{
System.out.println("Expenses for " + days + ":");
float exp = myScanner.nextFloat();
}
}
}