Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过将try-catch块放入do-while循环中来捕获异常_Java_Class_Exception_Try Catch_Java.util.scanner - Fatal编程技术网

Java 如何通过将try-catch块放入do-while循环中来捕获异常

Java 如何通过将try-catch块放入do-while循环中来捕获异常,java,class,exception,try-catch,java.util.scanner,Java,Class,Exception,Try Catch,Java.util.scanner,即使在发现输入类型不正确的InputMismatchException后,我仍想继续执行do while循环。我试着把它放到循环中,但它给了我一个错误。我只想知道如何通过在try catch块中放置try catch来继续do while循环 到目前为止,我得到了这个: package constructcompleteprogram; import java.util.Scanner; /** * Returns the fuel economy by measuring the num

即使在发现输入类型不正确的
InputMismatchException
后,我仍想继续执行
do while
循环。我试着把它放到循环中,但它给了我一个错误。我只想知道如何通过在
try catch
块中放置
try catch来继续
do while
循环

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

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");
    }
}


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

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

input.next();//您应该如何做什么?您提供了什么输入、得到了什么输出以及希望得到什么输出?
try {
    car = input.nextInt();
} catch (Exception e) {
    input.next();          // <-- this line "consumes" input
    System.out.println(e);
}