簡體   English   中英

無法捕獲 InputMismatchException

[英]Can't catch InputMismatchException

所以我一直在完成一項任務,要求我們建立一個“患者經理”,根據緊急程度確定首先治療的患者。 我遇到的主要問題是,當用戶在緊急列中輸入除數字之外的其他內容時,它會使程序崩潰並給我一個 InputMismatchException,盡管我試圖抓住它。 任何想法都非常感謝!

import java.util.*;

    public class PatientManager {
        private PriorityQueue<Patient> waitingList;

        public PatientManager() {
            waitingList = new PriorityQueue<Patient>();
        }

        // Starter method
        public void start() {
            String choice;
            Scanner in = new Scanner(System.in);
            String name;
            int emergency = 0;
            int order = 0;
            Patient patient;
            System.out.println("-------------------------------");
            System.out.println("(1) New Patient.");
            System.out.println("(2) Next Patient.");
            System.out.println("(3) Waiting List.");
            System.out.println("(4) Exit.");
            System.out.println("-------------------------------");

            while (true) {
                System.out.print("* Choose an item from the menu:");
                choice = in.next();
                switch (choice) {
                case "1":
                    System.out.println("Enter patient's name:");
                    name = in.next();
                    System.out.println("Enter emergency [1 (low) to 5 (life-and-death)]:");

                    while (true) {
                        try {
                            emergency = in.nextInt();
                            if (emergency >= 1 && emergency <= 5)

                                break;
                            else {
                                System.out.println("(x) Wrong value. Try again:");
                                emergency = in.nextInt();
                            }
                        } catch (Exception e) {
                            System.out.println("(x) Wrong value. Try again:");
                            emergency = Integer.parseInt(in.nextLine());
                        }
                    }
                    order++;
                    patient = new Patient(order, name, emergency);
                    waitingList.add(patient);
                    System.out.println("Patient added to the waiting list.");
                    break;
                case "2":
                    if (waitingList.isEmpty()) {
                        System.out.println("No more patients.");
                    } else {
                        patient = waitingList.remove();
                        System.out.println(patient.getName() + " is treated.");
                    }
                    break;
                case "3":
                    if (waitingList.size() == 0) {
                        System.out.println("No patients in the list.");
                    } else {
                        System.out.println("Waiting list includes:");
                        Iterator<Patient> iterator = waitingList.iterator();
                        while (iterator.hasNext()) {
                            patient = (Patient) iterator.next();
                            System.out.println("- " + patient.toString());
                        }
                    }
                    break;
                case "4":
                    System.out.println("Program terminated. Good bye!!");
                    in.close();
                    System.exit(0);
                    break;
                default:
                    System.out.println("(x) Wrong choice.");
                    break;
                }
            }


        }
    }

首先,您的代碼比它需要的復雜得多。 用這個替換整個 while 循環,然后再次測試。

while (emergency < 1 || emergency > 5) {
    try {
        emergency = in.nextInt();
        if (emergency < 1 || emergency > 5)
            System.out.println("(x) Wrong value. Try again:");
    }
    catch (Exception e) {
        System.out.println("(x) Wrong value. Try again:");
    }
}

我設法解決了它。 我通過將 in.next() 添加到 catch 塊中來清除掃描儀的結果。 所以它看起來像這樣:

catch (Exception e) {
                    System.out.print("(x) Wrong value. Try again:");
                    in.next();
                    System.out.println(emergency);
                }

暫無
暫無

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

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