即使在找到do-while
之后,当输入的类型不正确时,我想继续InputMismatchException
循环。我试着把它放在循环中,但它给了我一个错误。我只是想知道如何通过在其中放置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);
}
}
答案 0 :(得分:0)
你的循环不起作用,因为一旦插入了错误的输入,就会在进一步的try {
car = input.nextInt();
} catch (Exception e) {
input.next(); // <-- this line "consumes" input
System.out.println(e);
}
语句之间传递。在第一次(和每次连续)不匹配后,不会从控制台删除/取消错误输入。所以你需要添加一行,这将消耗&#34;它,例如:
//listen to ajax start event
$( document ).ajaxStart(function() {
$('#my_loader').fadeIn();
});
//listen to ajax complete event
$( document ).ajaxComplete(function() {
$('#my_loader').fadeOut();
});