gpt4 book ai didi

java - 在 If 语句中使用临时值继续 Try Catch

转载 作者:行者123 更新时间:2023-11-29 09:26:55 27 4
gpt4 key购买 nike

我需要输入到数组中的值是 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(),就像您在主循环中所做的那样。也就是说,您想接受 double 作为输入。另外,您的 try/catch 语法似乎有误。您想捕获可能由用户输入引起的错误;因此,您需要在用户在 try block 中输入内容的位置添加代码。见下文:

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;
}
}
}

关于java - 在 If 语句中使用临时值继续 Try Catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53695120/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com