簡體   English   中英

需要幫助循環我的try和catch語句在Java中

[英]Need help loop my try and catch statements in java

我正在調用此方法,並且在大多數情況下它都能完美地工作。 不知道這是否足以讓你們推斷並找出我的問題,但是我想我會試一試。

當我輸入一個超出數組范圍的整數或文件名不存在時,它將引發catch語句。 我希望它然后循環回到程序正在詢問的問題,而不僅是繼續執行程序的其余部分。.當我將catch語句與try語句放在同一while循環中時,我總是遇到錯誤。 感謝您的幫助,我希望這些內容足夠清晰,讓大家理解。

public static String [][] placeCustomer(String [][] MovieSeats, int rows, int columns, String database)
{
    //Get user data and then write the name to the array space specified by the user..
    Scanner input = new Scanner(System.in);

    try 
        {
            File readFile = new File(database);
            Scanner reader = new Scanner(readFile);

            while (reader.hasNextLine())
            {
                String user = reader.nextLine();
                System.out.println(user + " wants to sit in the theater. Where would you like to place him?");
                String lastUser = user;

                System.out.print("Row: ");
                int placeRow = input.nextInt();

                System.out.print("Column: ");
                int placeCol = input.nextInt();


                while (!MovieSeats[placeRow][placeCol].equals("Seat Empty |")) //If element in 2-D array reads empty, then tell user.
                {
                    System.out.println("Sorry that seat is already taken.. try a different location.."); //Give them another chance to change location
                    System.out.println("Please enter a new location for " + user);
                    System.out.print("Row: ");

                    placeRow = input.nextInt();

                    System.out.print("Column: ");
                    placeCol = input.nextInt();

                    if (MovieSeats[placeRow][placeCol].equals("Seat Empty |")) //If it is empty, allow user to fill the 2-D element..
                    {
                        break;
                    }

                }

                if (MovieSeats[placeRow][placeCol].equals("Seat Empty |"))
                {


                    while (MovieSeats[placeRow][placeCol].equals("Seat Empty |")) 
                    {
                        System.out.println("The customer " + user + " has been placed at row " + placeRow + " and the column " + placeCol + ".");
                        System.out.println();


                        MovieSeats[placeRow][placeCol] = user;

                        System.out.println("The current seating \n________________________");

                        viewFilledTheater(MovieSeats, rows, columns);

                        System.out.println();
                    }
                }

                else 
                {
                    System.out.println("Please enter a valid value for the program to understand where you would like to place the customer...");   
                }


            }
        }



            //If the file does not exist, then catch the exception, print this statement and exit the program..

            catch (FileNotFoundException e) 
            {
                System.out.println("The movie theater will remain empty because \nwe cannot find the customer list with the name you provided..");

            }

            catch (ArrayIndexOutOfBoundsException e)
            {
                System.out.println("I am sorry, but the integer you entered is not within the proper bounds of the theater..");
            }







        return MovieSeats;

同時,我對您的代碼進行了詳細介紹,我認為您可以使其更簡單。 您希望ArrayIndexOutOfBoundsException ,然后終端重新詢問客戶端以輸入placeRow,placeCol,因此,您應將ArrayIndexOutOfBoundsException catch子句放在while循環內,而將FileNotFoundException catch子句放在while循環外。

以下是有關如何放置ArrayIndexOutOfBoundsException try-catch子句以滿足您需要的簡單演示

while(true){
    System.out.println(user
    + " wants to sit in the theater. Where would you like to place him?");
    String lastUser = user;
    System.out.print("Row: ");
    int placeRow = input.nextInt();

    System.out.print("Column: ");
    int placeCol = input.nextInt();
    try{
        if(!MovieSeats[placeRow][placeCol].equals("Seat Empty |")){
            System.out.println("Sorry that seat is already taken.. try a different location..");
            System.out.println("Please enter a new location for "+ user);
            continue;
            }else{
            //set this seat occupied
            break;
        }

        }catch(ArrayIndexOutOfBoundsException e){
        //e.printStackTrace();
        continue;
    }

}

因此,首先,您引發由catch語句捕獲的異常(這就是為什么將它們稱為catch語句的原因)。 您的問題實際上只是范圍界定之一。 將您的try / catch嵌套在相關循環中。 請注意,發生異常后,程序將在catch塊之后繼續執行。 如果需要,您可以具有多個嵌套的try / catch語句。

您應該構建一個執行此操作的遞歸方法:

步驟1.檢查座位是否可用。 如果有座位,請放置用戶並顯示。

步驟2.如果沒有座位,請詢問用戶是否喜歡重新輸入他們的選擇。 如果是,請轉到步驟1。如果否,請退出。

這樣,無論用戶選擇了多少次錯誤的值,都將始終選擇重新輸入。 除非用戶選擇,否則您的程序將永遠不會退出。

我希望這能給您一些想法。 祝好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM