我确信这是我无法发现的简单事情,我有一个do while循环,提示用户输入数组大小,该大小将用于程序的其余部分。 如果用户输入正确的输入,程序将继续运行并运行良好,但是如果用户输入错误的输入...
public static void main(String[] args)
{
// user enters up to 20 double values, stored in an array, user should enter 99999 to quit entering numbers. If user has not entered any numbers yet
// display an error message, otherwise, display each entered value and it's distance from the average
Scanner keyboard = new Scanner(System.in);
int arraySize = 0;
boolean isValid = false;
do
{
isValid = true;
arraySize = 0; // reset these values at start of each loop.
System.out.println("Enter an array size.");
try {
arraySize = keyboard.nextInt();
}
catch(NegativeArraySizeException mistake) {
System.out.println("Do not enter a negative number for the arrays size.");
System.out.println();
isValid = false;
}
catch(InputMismatchException mistake) {
System.out.println("Make sure to enter a valid number.");
System.out.println();
isValid = false;
}
} while (isValid == false);
如果用户输入了无效的输入(例如"红色"),则catch块将弹出并显示"请确保输入有效的数字"。 和"输入数组大小"。 一遍又一遍,而没有给用户提供实际输入任何输入的机会。 我认为重置arraySize变量可以解决此问题,但不能解决。 我想键盘缓冲区里有东西,但是到目前为止,空的printlns组合还没有奏效。
我听说不应将异常用于验证用户输入。 这是为什么?
无论如何,它与这个问题无关,因为它是异常处理中的一个练习。
mmuzahid快要出现了。 我添加了一种检查输入值是否为负的方法。 请检查
不使用isValid布尔变量并输入简单代码。
int arraySize = 0;
do {
System.out.println("Enter a valid array size.");
try {
arraySize = Integer.valueOf(keyboard.nextLine());
if (arraySize < 0) throw new NegativeArraySizeException();// for negative arry size
break;// loop break when got a valid input
} catch (Exception mistake) {
System.err.println("Invalid input:" + mistake);
}
} while (true);
会"休息"吗? 曾经引起任何问题吗? 我知道某些东西,例如" goto"之类的东西可能会引起问题。 如果没有,那似乎是建立"无限"循环的一种非常优雅的方法,我将采用这种方法作为解决此类问题的方法。
@SpaceOstrich Java中没有goto。 当使用有效的非负整数设置数组大小时,我已经使用break语句进行循环退出。 可能是错误的,但是您可以通过这种方式减少代码以仅接受有效输入。
您可以添加keyboard.nextLine(); 如果出现异常,则应该解决该问题。
try {
arraySize = keyboard.nextInt();
}
catch(NegativeArraySizeException mistake) {
System.out.println("Do not enter a negative number for the arrays size.");
System.out.println();
isValid = false;
keyboard.nextLine();
}
catch(Exception mistake) {
System.out.println("Make sure to enter a valid number.");
System.out.println();
isValid = false;
keyboard.nextLine();
}
这样就解决了。 我一直以为添加额外的打印行毕竟不是清除键盘缓冲区的方法,而是添加了额外的nextLines。 睡眠不足再次发作。
使用keyboard.nextLine()和NumberFormatException
do {
// more code
try {
arraySize = Integer.valueOf((keyboard.nextLine()));
} catch (NegativeArraySizeException mistake) {
// more code
isValid = false;
} catch (InputMismatchException mistake) {
// more code
isValid = false;
} catch (NumberFormatException mistake) {
// more code
isValid = false;
}
} while (isValid == false);
[来自Scanner类的next()和nextLine()方法之间有什么区别?](stackoverflow.com/questions/22458575/)
mmuzahid快要出现了。 但是我还添加了一种检查负数的方法。 尝试这个
Scanner keyboard = new Scanner(System.in);
int arraySize = 0;
boolean isValid = false;
do {
System.out.println("Enter a valid array size.");
try {
arraySize = Integer.valueOf(keyboard.nextLine());
if (arraySize < 0) {
System.out.println("Make sure to enter a valid positive number.");
} else {
break;
}
} catch (Exception mistake) {
System.out.println("Make sure to enter a valid number. Error:" + mistake);
}
} while (true);
请查看此修复程序是否适合您。 当您尝试从nextInt函数获取字符串时,扫描仪出现问题。 在此,我已获取字符串并解析为Integer,然后处理了Number格式异常
public static void main(String[] args) {
// user enters up to 20 double values, stored in an array, user should enter 99999 to quit entering numbers. If user has not entered any numbers yet
// display an error message, otherwise, display each entered value and it's distance from the average
Scanner keyboard = new Scanner(System.in);
int arraySize = 0;
boolean isValid = false;
do {
isValid = true;
arraySize = 0; // reset these values at start of each loop.
System.out.println("Enter an array size.");
try {
arraySize = Integer.parseInt(keyboard.next());
} catch (NegativeArraySizeException mistake) {
System.out.println("Do not enter a negative number for the arrays size.");
System.out.println();
isValid = false;
} catch (InputMismatchException mistake) {
System.out.println("Make sure to enter a valid number.");
System.out.println();
isValid = false;
} catch (NumberFormatException nfe) {
System.out.println("Make sure to enter a valid number.");
System.out.println();
isValid = false;
}
} while (isValid == false);
}