[英]How to keep continue the same line if user input wrong?
当我运行代码时,如果你输入字符而不是整数,它会提示再次重新输入。 但是当我们回答第 2 或第 3 个问题时,如果错了,它会要求您回到第 1 个问题。 有什么办法可以解决吗? 非常感谢!
while (true) {
try {
Scanner input = new Scanner(System.in);
System.out.println("Enter your monthly income (after tax and insurance): ");
float monthlyIncome = input.nextFloat();
System.out.println("Enter monthly cost for food: ");
float monthlyFoods = input.nextFloat();
System.out.println("Enter monthly cost for rent: ");
float monthlyRent = input.nextFloat();
System.out.println("Enter monthly cost for utilities " +
"(e.g. gas, phone bills, Internet): ");
float monthlyUtilities = input.nextFloat();
System.out.println("Enter monthly cost for shopping and hobbies: ");
float monthlyHobbies = input.nextFloat();
System.out.println("Do you plan for saving? (Enter 0 if not): ");
float monthlySaving = input.nextFloat();
monthlyExpenses(monthlyIncome, monthlyFoods, monthlyRent,
monthlyUtilities, monthlyHobbies, monthlySaving);
break;
} catch (InputMismatchException exception) {
System.out.println("Please enter number only!");
}
}
public static float getFloat(String message)
{
float value;
while (true) {
try {
Scanner input = new Scanner(System.in);
System.out.println(message);
value=input.nextFloat();
} catch (InputMismatchException exception) {
System.out.println("Please enter number only!");
}}
return value;
}
float monthlyIncome = getFloat("Enter your monthly income (after tax and insurance): ");
//类似这样的
使用您需要重复的代码,以防 while 循环中出现错误; 这使您的代码看起来像这样:
while (true) {
float value;
try {
// code to get proper value from user & store in `value`
return value;
} catch (InputMismatchException exception) {
System.out.println("Please enter number only!");
}
}
现在只需将此代码移动到一个方法:
float getFloatValue(String questionDescription){
while (true) {
float value;
try {
Scanner input = new Scanner(System.in);
System.out.println(questionDescription+": ");
value = input.nextFloat();
return value;
} catch (InputMismatchException exception) {
System.out.println("Please enter number only!");
}
}
return Float.NaN;
}
所以最后获取值的代码如下所示:
float monthlyIncome = getFloatValue("Enter your monthly income (after tax and insurance)");
float monthlyFoods = getFloatValue("Enter monthly cost for food");
float monthlyRent = getFloatValue("Enter monthly cost for food");
// get other values same as above
monthlyExpenses(monthlyIncome, monthlyFoods, monthlyRent,
monthlyUtilities, monthlyHobbies, monthlySaving);
这种方法的好处是只接受有效的浮点值,并且您的代码看起来也很干净。
另一种可能性是将所有问题保存到一个数组中,并使用一个变量作为索引来计算传递了多少输入。 然后在第二个数组中保存输入,最后你只需要转换它。
优点:这是高度可定制的,可以很容易地扩展。
代码看起来像那样。
// Lots of Code here
Scanner scan = new Scanner(System.in);
int count = 0;
String[] questions = {"How high...?", "[Insert questions 2]"}; // Put in all your questions here
float[] answers = new float[questions.length]; // array for answers
while(count < questions.length) {
try {
System.out.println(questions[count]);
answers[count] = Float.parseFloat(scan.nextLine());
count++;
} catch (Exception e) {
System.out.println("Bad doggo! No food for you tonight!"); // Tell the user he was a bad guy
}
}
scan.close();
// If you want you can convert the answer array back to variables here but I think this way is easier
// More code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.