Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception handling 需要破例。我想在用户在数组中输入非数值时捕获一个错误_Exception Handling - Fatal编程技术网

Exception handling 需要破例。我想在用户在数组中输入非数值时捕获一个错误

Exception handling 需要破例。我想在用户在数组中输入非数值时捕获一个错误,exception-handling,Exception Handling,谁能帮我破例一下吗。我想在用户在数组中输入非数值时捕捉一个错误。在它捕捉到错误后,我想让用户重试并再次输入,这次是一个整数 假设JAVA, 您所要做的就是将I put作为字符串,然后通过integer.parseInt()将字符串转换为整数 如果parseInt没有抛出NumberFormatException,则用户输入一个数字输入,将其添加到数组中,如果没有,则再次获取一个输入。如果您需要在一个简单的应用程序中执行此操作以测试您的异常处理技能,下面是一个示例代码,该代码使用整数填充数组,并期

谁能帮我破例一下吗。我想在用户在数组中输入非数值时捕捉一个错误。在它捕捉到错误后,我想让用户重试并再次输入,这次是一个整数

假设JAVA, 您所要做的就是将I put作为字符串,然后通过
integer.parseInt()
将字符串转换为整数


如果parseInt没有抛出NumberFormatException,则用户输入一个数字输入,将其添加到数组中,如果没有,则再次获取一个输入。如果您需要在一个简单的应用程序中执行此操作以测试您的异常处理技能,下面是一个示例代码,该代码使用整数填充数组,并期望使用int作为输入。当给定另一个值时,代码会提示用户重试

int[] array = new int[10];

Scanner sc = new Scanner(System.in);
boolean shouldAskAgain = true;
int index = 0;
do{
    System.out.println("Enter an integer value for the array");
    try{
        array[index] = sc.nextInt();
        index++;
        if(index == array.length-1)
            shouldAskAgain = false;
    }
    catch(InputMismatchException e) {
        System.out.println("Wrong input. Try again");
        sc.nextLine();
    }
}while(shouldAskAgain);

对不起,这是超宽的。也许如果你添加一些不起作用的示例代码,我们可以提供帮助。在任何特定的编程语言中?