繁体   English   中英

如果发现错误,从头开始再次运行程序(在while循环中尝试捕获)Java

[英]Run program again from the start if error was found (try-catch inside while loop) Java

程序说明:用户为数组输入元素,然后返回数组中间的2个元素


我正在尝试在 while 循环中实现 try-catch 语句。 这个想法是,当它发现错误时,程序会再次开始运行(从输入数组中元素的数量开始)。 问题是我的代码中的 while 循环适用于 else 语句:

} else {
                System.out.println("ERROR: Incompatible Array Size ");
                runAgain = true;
            }
            }`

但是对于 catch 表达式,我最终陷入了无限循环


代码:

公共 class MiddleArray {

public static int[] makeMiddle(int[] nums) {

    int[] newList = new int[2];

    if (nums.length>2) {

        int middle = nums.length/2;
        newList[0] = nums[middle-1];
        newList[1] = nums[middle];
        return newList;
    }

    else {
        return nums;
    }

}

public static void main(String[] args) {
    int  numOfElements; //number of elements in the array
    int userInput;
    boolean runAgain;


    Scanner input = new Scanner(System.in);

    System.out.print("Please enter the number of elements that you wanna have in array: ");



    try {
        numOfElements = input.nextInt();

        if (numOfElements % 2 == 0 && numOfElements != 0) {
            int[] arrayOfNumbers = new int[numOfElements];


            for (int i = 0; i <= arrayOfNumbers.length - 1; i++) {
                System.out.print("Element " + (i+1) + ": ");
                userInput = input.nextInt();
                arrayOfNumbers[i] = userInput;
            }
            int[] middleOfArray = makeMiddle(arrayOfNumbers); //return

            System.out.print("For the array " +
                    Arrays.toString(arrayOfNumbers) + " the 2 middle elements is " + Arrays.toString(middleOfArray));
        } else {
            System.out.print("ERROR: Incompatible Array Size ");
        }

    }

    catch(InputMismatchException e) {
        System.out.println("ERROR: Incompatible Data Type");
    }
    catch (NegativeArraySizeException e) {
        System.out.println("ERROR: Incompatible Array Size");
    }
}

}

添加 do.. while 指令:

do {
   try {
        numOfElements = input.nextInt();

        if (numOfElements % 2 == 0 && numOfElements != 0) {
            int[] arrayOfNumbers = new int[numOfElements];


            for (int i = 0; i <= arrayOfNumbers.length - 1; i++) {
                System.out.print("Element " + (i+1) + ": ");
                userInput = input.nextInt();
                arrayOfNumbers[i] = userInput;
            }
            int[] middleOfArray = makeMiddle(arrayOfNumbers); //return

            System.out.print("For the array " +
                    Arrays.toString(arrayOfNumbers) + " the 2 middle elements is " + Arrays.toString(middleOfArray));
            return; // !!!THIS STOP WHILE!!!
        } else {
            System.out.print("ERROR: Incompatible Array Size ");
        }

    }

    catch(InputMismatchException e) {
        System.out.println("ERROR: Incompatible Data Type");
    }
    catch (NegativeArraySizeException e) {
        System.out.println("ERROR: Incompatible Array Size");
    }
} while(true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM