【问题标题】:How to catch the exception, by putting try-catch block inside the do-while loop如何通过将 try-catch 块放在 do-while 循环中来捕获异常
【发布时间】:2015-07-30 15:26:25
【问题描述】:

即使在找到InputMismatchException 之后,当输入的类型不正确时,我也想继续我的do-while 循环。我试着把它放在循环中,但它给了我一个错误。我只想知道如何继续 do-while 循环,方法是在其中放置一个 try-catch 块。

到目前为止,我得到了这个:

package constructcompleteprogram;

import java.util.Scanner;

/**
 * Returns the fuel economy by measuring the number of passengers, fuel
 * capacity, miles per gallon and range according by the users input.
 *
 * @author sandeepshahi
 * @version 1.0
 * @since 2015-04-15
 * @see Vehicle
 */
/**
 *
 * @param passenger This is the first parameter to the void method
 * @param mpg This is the second parameter to the void method
 * @param range This is the third parameter to the void method
 *
 * @return pass This method holds its argument and let the user input number of
 * passenger.
 * @return fuelCap This method holds its argument and let the user input the
 * fuel capacity.
 * @return milesPer This method holds its argument and let the user to input the
 * miles per gallon.
 * @retrun range This method calculates the range of the vehicle.
 */
class Vehicle {
    Scanner input = new Scanner(System.in);
    int passenger;
    double fuelcap;
    double mpg;
    double range;
    static int car;

    void pass() {
        System.out.println("   enter the Number of passenger");
        passenger = input.nextInt();
        System.out.println("you entered: " + passenger);
    }

    void fuelCap() {
        System.out.println("   enter fuel capacity in integer, please");
        fuelcap = input.nextInt();
        System.out.println("you entered: " + fuelcap);
    }

    void milesPer() {
        System.out.println("   enter miles for gamallon please");
        mpg = input.nextDouble();
        System.out.println("  you entered: " + mpg);
    }

    void range() {
        System.out.println("   The car has a range of " + (fuelcap * mpg) + " miles");
    }
}

/**
 * This class will create three different vehicles and let the user choose the
 * car and execute the void methods for each vehicle to print its specific
 * argument.
 *
 * Once the user is done choosing the car, user is able to go through various
 * options to get the vehicle description or may exit the menu anytime to choose
 * a different car or to exit the whole program.
 */
public class Constructcompleteprogram {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Vehicle ferrari = new Vehicle();// creates a new vehicle
        Vehicle Lamborghini = new Vehicle();// creates a new vehicle
        Vehicle Bugatti = new Vehicle(); // creates a new vehicle
        int option; // variable
        int car;

        do {
            System.out.println(
                    "\n**************************************************"
                    + "\n Select your Car type                            *"
                    + "\n  1)Ferrari                                      *"
                    + "\n  2)Lamborghini                                  *"
                    + "\n  3)Bugatti                                      *"
                    + "\n  4)Exit                                         *"
                    + "\n**************************************************");
            try {
                car = input.nextInt();
            } catch (Exception e) {
                System.out.println(e);
            }

            switch (car) {
                case 1:
                    System.out.println("you selected Ferrari");
                    do {
                        System.out.println(
                                  "\n**************************************************"
                                + "\n Main menu:                                      *"
                                + "\n Enter # to run program or Quite:                *"
                                + "\n 1)Enter the number of passenger                 *"
                                + "\n 2) Enter fuel capacity                          *"
                                + "\n 3) Enter Miles per Gallon                       *"
                                + "\n 4) Calculate range                              *"
                                + "\n 5) Vehicle discription                          *"
                                + "\n 6) Quit/change Car                              *"
                                + "\n**************************************************");

                        option = input.nextInt();// choose an option from the menu
                        switch (option) {
                            case 1:
                                System.out.println("   You selected Option 1 ");
                                ferrari.pass();
                                break;
                            case 2:
                                System.out.println("   You selected Option 2 ");
                                ferrari.fuelCap();
                                break;
                            case 3:
                                System.out.println("   You selected Option : ");
                                ferrari.milesPer();
                                break;
                            case 4:
                                System.out.println("   You selected Option 4 ");
                                ferrari.range();
                                break;
                            case 5:
                                System.out.println("   You selected Option 5 to see the results");
                                System.out.println();
                                System.out.println("   The FERRARI carries: " + ferrari.passenger);
                                System.out.println("   The FERRARI has a fuel capacity of: " + ferrari.fuelcap);
                                System.out.println("   The FERRARI mpg: " + ferrari.mpg);
                                ferrari.range();
                                break;
                            case 6:
                                System.out.println(" you are quitting the program/you may chose different car type ");
                                break;
                            default:
                                System.out.println("Not an Option/select Again");
                                break;
                        }
                    } while (option != 6);
                    break;
                case 2:
                    System.out.println("you selected Lamborghini");
                    do {
                        System.out.println(
                                  "\n**************************************************"
                                + "\n Main menu:                                      *"
                                + "\n Enter # to run program or Quite:                *"
                                + "\n 1)Enter the number of passenger                 *"
                                + "\n 2) Enter fuel capacity                          *"
                                + "\n 3) Enter Miles per Gallon                       *"
                                + "\n 4) Calculate range                              *"
                                + "\n 5) Vehicle discription                          *"
                                + "\n 6) Quit/change car                              *"
                                + "\n**************************************************");

                        option = input.nextInt();
                        switch (option) {
                            case 1:
                                System.out.println("   You selected Option 1 ");
                                Lamborghini.pass();
                                break;
                            case 2:
                                System.out.println("   You selected Option 2 ");
                                Lamborghini.fuelCap();
                                break;
                            case 3:
                                System.out.println("   You selected Option : ");
                                Lamborghini.milesPer();
                                break;
                            case 4:
                                System.out.println("   You selected Option 4 ");
                                Lamborghini.range();
                                break;
                            case 5:
                                System.out.println("   You selected Option 5 to see the results");
                                System.out.println();
                                System.out.println("   The LAMBORGHINI carries: " + Lamborghini.passenger);
                                System.out.println("   The LAMBORGHINI has a fuel capacity of: " + Lamborghini.fuelcap);
                                System.out.println("   The LAMBORGHINI mpg: " + Lamborghini.mpg);
                                Lamborghini.range();
                                break;
                            case 6:
                                System.out.println(" you are quitting the program/you may chose different car type ");
                                break;
                            default:
                                System.out.println("Not an Option/select Again");
                                break;
                        }
                    } while (option != 6);
                    break;
                case 3:
                    System.out.println("you selected Bugatti");
                    do {
                        System.out.println(
                                "\n**************************************************"
                                + "\n Main menu:                                      *"
                                + "\n Enter # to run program or Quite:                *"
                                + "\n 1)Enter the number of passenger                  *"
                                + "\n 2) Enter fuel capacity                          *"
                                + "\n 3) Enter Miles per Gallon                       *"
                                + "\n 4) Calculate range                              *"
                                + "\n 5) Vehicle discription                          *"
                                + "\n 6) Quit/change car                              *"
                                + "\n**************************************************");

                        option = input.nextInt();
                        switch (option) {
                            case 1:
                                System.out.println("   You selected Option 1 ");
                                Bugatti.pass();
                                break;
                            case 2:
                                System.out.println("   You selected Option 2 ");
                                Bugatti.fuelCap();
                                break;
                            case 3:
                                System.out.println("   You selected Option : ");
                                Bugatti.milesPer();
                                break;
                            case 4:
                                System.out.println("   You selected Option 4 ");
                                Bugatti.range();
                                break;
                            case 5:
                                System.out.println("   You selected Option 5 to see the results");
                                System.out.println();
                                System.out.println("   The BUGATTI carries: " + Bugatti.passenger);
                                System.out.println("   The BUGATTI has a fuel capacity of: " + Bugatti.fuelcap);
                                System.out.println("   The BUGATTI mpg: " + Bugatti.mpg);
                                Bugatti.range();
                                break;
                            case 6:
                                System.out.println(" you are quitting the program/you may chose different car type ");
                                break;
                            default:
                                System.out.println("Not an Option/select Again");
                                break;
                        }
                    } while (option != 6);
                    break;
                case 4:
                    System.out.println("You  Exited from the program");
                    break;
                default:
                    System.out.println("Not an option/Choose your option to select the car");
                    break;
            }
        } while (car != 4);
    }
}

【问题讨论】:

  • 你应该怎么做?你提供什么输入,你得到什么输出你期望什么输出?

标签: java class exception try-catch java.util.scanner


【解决方案1】:

您的循环不起作用,因为一旦插入不正确的输入,就会在进一步的car.nextInt() 语句之间传递。在第一次(以及每个连续的)不匹配后,不正确的输入不会从控制台中删除/取消。所以你需要添加一行,它会“消耗”它,例如:

try {
    car = input.nextInt();
} catch (Exception e) {
    input.next();          // <-- this line "consumes" input
    System.out.println(e);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多