java - 在 If 语句中使用临时值继续尝试 Catch
问题描述
我需要输入到数组中的值是 1-100 而不是任何字母。我试图设置一个临时值,该值将捕获之前不是 1-100 但似乎无法使其工作的数字。如果输入字母,我会遇到程序将关闭的问题。任何帮助,将不胜感激。
public class FocusGroup {
static Scanner scan = new Scanner(System.in);
public static void main(String args[]) {
double tempValue = scan.nextDouble();
if (tempValue > 100 || tempValue < 0) {
scan.nextDouble();
System.out.println("Invalid");
} else {
while (true) {
try {
double sumFocus = 0;
double[] arrayFocus = new double[2];
for (int i = 0; i < 2; i++) {
arrayFocus[i] = scan.nextDouble();
}
for (double num : arrayFocus) {
sumFocus = sumFocus + num;
}
}
catch (InputMismatchException e) {
System.out.println("Invalid input");
scan.nextLine();
}
}
}
}
}
解决方案
在您的 catch 语句中,您使用scan.nextLine()
,但您应该使用scan.nextDouble()
,就像您在主循环中所做的那样。也就是说,您希望接受双精度数作为输入。此外,您的 try/catch 语法似乎是错误的。您想捕获可能由用户输入引起的错误;因此,您需要在用户在try块中输入内容的位置添加代码。见下文:
try {
double tempValue = scan.nextDouble();
//this loop will keep spinning, until tempValue is valid
while (tempValue > 100d || tempValue < 0d) {
System.out.println("Invalid. Try again. Range is 0 - 100");
tempValue = scan.nextDouble();
}
//this loop will spin forever (There is nothing to terminate it)
while (true) {
double sumFocus = 0;
double[] arrayFocus = new double[2];
for (int i = 0; i < 2; i++) {
arrayFocus[i] = scan.nextDouble();
}
for (double num : arrayFocus) {
sumFocus = sumFocus + num;
}
}
}
catch (InputMismatchException e) {
System.out.println("Invalid input");
scan.nextDouble(); //this doesn't do anything. The value is not assigned to any variable
}
使用上面的代码,当用户输入一个无效字符时,例如:一个字母,程序就会停止。为什么?因为catch在无限循环之外,这使得你的程序计数器移动到循环之外,并且当catch语句中的代码被执行时,你的程序也会在到达结束时终止。
你可以做的是这样的:
static double validValueWithRange(Double min, Double max){
Scanner s = new Scanner(System.in);
System.out.println("Enter a valid value for a 'double': ");
double value;
try {
value = s.nextDouble();
if(value > max || value < min){
System.out.println("Invalid. Try again. Range is 0 - 100");
return validValueWithRange(min, max);
}
}
catch (InputMismatchException e) {
System.out.println("Invalid input. Try again.");
return validValueWithRange(min, max);
}
return value;
}
static double validValue(){
Scanner s = new Scanner(System.in);
System.out.println("Enter a valid value for a 'double': ");
double value;
try {
value = s.nextDouble();
}
catch (InputMismatchException e) {
System.out.println("Invalid input. Try again.");
return validValue();
}
return value;
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double tempValue = validValueWithRange(0d, 100d);
while (true) {
System.out.println("Now entering endless while loop");
double sumFocus = 0;
double[] arrayFocus = new double[2];
for (int i = 0; i < 2; i++) {
arrayFocus[i] = validValue();
}
for (double num : arrayFocus) {
sumFocus = sumFocus + num;
}
}
}
推荐阅读
- android - Data Logic SDK Integration throws Java.Lang.NoClassDefFoundError
- r - R 'data' 的问题必须是向量类型,是 'NULL'
- ios - 使用 numberOfRowsInSection 在 Swift 中的 void 函数中出现意外的非 void 返回值
- ruby-on-rails - 在 Ruby on Rails 中使用强参数验证输入文件参数
- python - Reducing entry letters using tkinter not working
- javascript - 在 javascript 标记中,在 ajax/jquery 代码中显示逗号而不是点
- html - Horizontal lines behind buttons HTML/CSS
- arrays - PL/SQL - Declare a record where keep records of a table and an operation?
- python-3.x - 从 db 文件转换为 csv 并访问列式数据时,列表索引超出范围
- java - 在服务级别启动浏览器时,applet 调用的 Java 运行时不会观察所有 JRE 配置