簡體   English   中英

不能多次捕獲異常

[英]cannot catch the exception more than one time

在運行代碼時,如果我輸入數字以外的任何字符,則將捕獲“ java.util.InputMismatchException”,但是下次如果我輸入數字以外的其他字符,則程序將終止,並出現錯誤“線程異常”。 “ java.util.InputMismatchException”。 如何使它能夠捕獲多個並發異常,直到給出有效輸入為止。

/*
Twenty students were asked to rate on a scale of 1 to 5 the quality of the food in the
student cafeteria, with 1 being “awful” and 5 being “excellent.” Place the 20 responses
in an integer array and determine the frequency of each rating.
 */
import java.util.Scanner;

public class StudentPoll 
{
    static Scanner input=new Scanner(System.in);
    public static void main(String[] args)
    {
        System.out.println("Rate the quality of food from 1 to 5." +
                "\n1 being “awful” and 5 being “excellent”.\n");
        int[] array=new int[10];
        int num =0;
        for(int i=0;i<array.length;i++)
        {

            do{
                System.out.println("Student "+(i+1)+" Enter Your Response:");
                try
                {
                    num=input.nextInt();
                }
                catch(java.util.InputMismatchException e)
                {
                    System.out.println("Enter numbers only.");
                    input.nextLine();
                    num=input.nextInt();
                }   
                if(num<=0 || num>5)

                {
                    System.out.println("Enter 1 to 5 only.");
                }
            }while(num<=0 || num>5);
            array[i]=num;

        }

        int[] frequency=new int[6];

        for ( int i = 0; i < array.length; i++ )
        {
            frequency[array[i]]=frequency[array[i]]+1;      
        }

        System.out.printf("*    :%d (awful)\n",frequency[1]);
        System.out.printf("**   :%d\n",frequency[2]);
        System.out.printf("***  :%d\n",frequency[3]);
        System.out.printf("**** :%d\n",frequency[4]);
        System.out.printf("*****:%d (excellent)\n",frequency[5]);
    }
}`

由於catch語句中沒有其他try-catch語句,因此,如果要再次捕獲該異常,可以重新運行循環。 因此,您將替換為:

catch(java.util.InputMismatchException e)
{
    System.out.println("Enter numbers only.");
    input.nextLine();
    num = input.nextInt();
} 

與:

catch(java.util.InputMismatchException e)
{
    System.out.println("Enter numbers only.");
    input.nextLine();
    continue;
}

因為在try塊中發生第一個異常時,該異常才由catch塊捕獲。 如果用戶再次輸入無效的輸入,則會再次引發異常。 但是您的代碼中沒有機制可以捕獲該異常。 因此主線程停止了。 捕獲塊將不負責捕獲在該塊內引發的異常。

import java.util.Scanner;

public class StudentPoll
{
static Scanner input=new Scanner(System.in);
public static void main(String[] args)
{
    System.out.println("Rate the quality of food from 1 to 5." +
            "\n1 being “awful” and 5 being “excellent”.\n");
    int[] array=new int[10];
    int num =0;
    for(int i=0;i<array.length;i++)
    {

        do{
            System.out.println("Student "+(i+1)+" Enter Your Response:");
            try
            {
                num=input.nextInt();
            }
            catch(java.util.InputMismatchException e)
            {
                input.next(); // consume the leftover new line
                System.out.println("Enter numbers only.");
            }
            if(num<=0 || num>5)

            {
                System.out.println("Enter 1 to 5 only.");
            }

        }while(num<1 || num>5); // change in the logic
        array[i]=num;

    }

    int[] frequency=new int[6];

    for ( int i = 0; i < array.length; i++ )
    {
        frequency[array[i]]=frequency[array[i]]+1;
    }

    System.out.printf("*    :%d (awful)\n",frequency[1]);
    System.out.printf("**   :%d\n",frequency[2]);
    System.out.printf("***  :%d\n",frequency[3]);
    System.out.printf("**** :%d\n",frequency[4]);
    System.out.printf("*****:%d (excellent)\n",frequency[5]);
}
}  

您的問題語句是在輸入錯誤后程序不會等待下一個用戶輸入。您可以通過java.util.Scanner接受輸入並在do while內進行for循環來實現此目的。

試試下面的代碼,它應該可以根據您的需要工作。

/*
Twenty students were asked to rate on a scale of 1 to 5 the quality of the food in the
student cafeteria, with 1 being “awful” and 5 being “excellent.” Place the 20 responses
in an integer array and determine the frequency of each rating.
 */
import java.util.Scanner;

public class StudentPoll  {
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("Rate the quality of food from 1 to 5."
                + "\n1 being “awful” and 5 being “excellent”.\n");
        int[] array = new int[10];
        int num = 0;

        do {
            for (int i = 0; i < array.length; i++) {
                System.out.println("Student " + (i + 1)
                        + " Enter Your Response:");
                Scanner sc = new Scanner(System.in);
                try {
                    num = sc.nextInt();
                } catch (java.util.InputMismatchException e) {
                    System.out.println("Enter numbers only.");
                    num = 0;
                    --i;
                    continue;
                }
                if (num <= 0 || num > 5) {
                    System.out.println("Enter 1 to 5 only.");
                    num = 0;
                    --i;
                    continue;
                }
                array[i] = num;
            }
        } while (num <= 0 || num > 5);


        int[] frequency = new int[6];

        for (int i = 0; i < array.length; i++) {
            frequency[array[i]] = frequency[array[i]] + 1;
        }

        System.out.printf("*    :%d (awful)\n", frequency[1]);
        System.out.printf("**   :%d\n", frequency[2]);
        System.out.printf("***  :%d\n", frequency[3]);
        System.out.printf("**** :%d\n", frequency[4]);
        System.out.printf("*****:%d (excellent)\n", frequency[5]);
    }
}

暫無
暫無

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

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