簡體   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