在我的java应用程序中,这是一个简单的TollMachine模拟器,我必须在我的代码中实现try / catch代码。我设法在hasNextInt提示符下解决String输入,但是当我尝试使用try / catch执行相同操作时,我无法让程序进入catch部分。程序按原样进入无限循环,我真的需要使用try / catch方法。否则,我使用if(hasNextInt)/ else(替代方法)来摆脱字符串输入并阻止无限循环。
public class TollMachine {
static boolean running = true; //variable to control whether the program should run
static int userInput = -1; //variable to store the user's input
static String userInput2 = ""; //variable to store the input in case user does not enter an integer
static int motoTicketCount = 0; //variable to store the number of tickets sold(moto)
static int carTicketCount = 0; //variable to store the number of tickets sold(car)
static int vanTicketCount = 0; //variable to store the number of tickets sold(van)
static int truckTicketCount = 0;//variable to store the number of tickets sold(truck)
static int totalTicketCount = 0;//variable to store the number of total tickets sold
static Scanner keyb = new Scanner(System.in); //variable to detect the user's input
public static void main(String[] args) {
while (userInput != 0) //runs while userInput != 0
{
//This is the main menu, the user is prompted to choose an option
System.out.println("1. Motorcycle");
System.out.println("2. Car");
System.out.println("3. Van");
System.out.println("4. Truck");
System.out.println("5. Display total tickets sold.");
System.out.println("0. Exit");
try {
if (keyb.hasNextInt()) //checks whether the user inputs an integer
{
userInput = keyb.nextInt(); //stores the user's input to the userInput
if (userInput > 5 || userInput < 0) //if one of these conditions are true, the user selected an option
{ //that is not mentioned
System.out.println("Invalid input."); //displays the Invalid input error
} else {
//checks which option the user chose with multiple "if" conditions
if (userInput == 1) {
//Displays an appropriate message to the user, and increases the two counters by 1
System.out.println("Here is your motorcycle ticket.");
motoTicketCount++;
totalTicketCount++;
}
if (userInput == 2) {
//Displays an appropriate message to the user, and increases the two counters by 1
System.out.println("Here is your car ticket.");
carTicketCount++;
totalTicketCount++;
}
if (userInput == 3) {
//Displays an appropriate message to the user, and increases the two counters by 1
System.out.println("Here is your van ticket.");
vanTicketCount++;
totalTicketCount++;
}
if (userInput == 4) {
//Displays an appropriate message to the user, and increases the two counters by 1
System.out.println("Here is your truck ticket.");
truckTicketCount++;
totalTicketCount++;
}
if (userInput == 5) {
//Displays how many of each ticket were sold, and the total number of tickets sold
System.out.println("Motorcycle tickets sold: " + motoTicketCount);
System.out.println("Car tickets sold: " + carTicketCount);
System.out.println("Van tickets sold: " + vanTicketCount);
System.out.println("Truck tickets sold: " + truckTicketCount);
System.out.println("Total tickets sold: " + totalTicketCount);
}
if (userInput == 0) {
running = false; //terminates the program
}
}//end of else after the if (hasNextInt())
}//end of first if (hasNextInt())
}//end of try
catch (Exception e) //this SHOULD catch an invalid input error
{
System.out.println("Invalid input."); //displays invalid input in case the
userInput2 = keyb.next(); //discard the bad input
}
}
}
}
答案 0 :(得分:0)
在这里:
if (userInput >5 || userInput<0) //if one of these conditions are true, the user selected an option
{ //that is not mentioned
System.out.println("Invalid input."); //displays the Invalid input error
}
将System.out.println...
更改为Throw new Exception("Invalid input.");
注意:这并非真正正确使用异常,但它可能有助于您理解它。
在你的catch区块中:
catch(Exception ex){
System.out.println(ex.toString());
}
编辑:为了尝试理解异常的概念,我建议您尝试将输入检查更改为if(keyb.hasNext())
,然后尝试将输入转换为Integer.parseInt(keyb.next());
中的整数阻止并捕获结果NumberFormatException
。
此外,不是使用userInput2
变量来存储错误数据,您只需设置userInput = -1;
即可在catch函数中重新启动循环。
完整代码:
public class TollMachine
{
static boolean running = true; //variable to control whether the program should run
static int userInput = -1; //variable to store the user's input
static String userInput2 = ""; //variable to store the input in case user does not enter an integer
static int motoTicketCount = 0; //variable to store the number of tickets sold(moto)
static int carTicketCount = 0; //variable to store the number of tickets sold(car)
static int vanTicketCount = 0; //variable to store the number of tickets sold(van)
static int truckTicketCount = 0;//variable to store the number of tickets sold(truck)
static int totalTicketCount = 0;//variable to store the number of total tickets sold
static Scanner keyb = new Scanner(System.in); //variable to detect the user's input
public static void main(String[] args)
{
while (userInput != 0) //runs while userInput != 0
{
//This is the main menu, the user is prompted to choose an option
System.out.println("1. Motorcycle");
System.out.println("2. Car");
System.out.println("3. Van");
System.out.println("4. Truck");
System.out.println("5. Display total tickets sold.");
System.out.println("0. Exit");
try
{
/**
THIS THROWS YOUR EXCEPTION
**/
if (keyb.hasNext())
{
userInput = Interger.parseInt(keyb.next()); //stores the user's input to the userInput
if (userInput >5 || userInput<0) //if one of these conditions are true, the user selected an option
{ //that is not mentioned
System.out.println("Invalid input."); //displays the Invalid input error
}
else
{
//checks which option the user chose with multiple "if" conditions
if (userInput == 1)
{
//Displays an appropriate message to the user, and increases the two counters by 1
System.out.println("Here is your motorcycle ticket.");
motoTicketCount++;
totalTicketCount++;
}
if (userInput == 2)
{
//Displays an appropriate message to the user, and increases the two counters by 1
System.out.println("Here is your car ticket.");
carTicketCount++;
totalTicketCount++;
}
if (userInput == 3)
{
//Displays an appropriate message to the user, and increases the two counters by 1
System.out.println("Here is your van ticket.");
vanTicketCount++;
totalTicketCount++;
}
if (userInput == 4)
{
//Displays an appropriate message to the user, and increases the two counters by 1
System.out.println("Here is your truck ticket.");
truckTicketCount++;
totalTicketCount++;
}
if (userInput == 5)
{
//Displays how many of each ticket were sold, and the total number of tickets sold
System.out.println("Motorcycle tickets sold: "+motoTicketCount);
System.out.println("Car tickets sold: "+carTicketCount);
System.out.println("Van tickets sold: "+vanTicketCount);
System.out.println("Truck tickets sold: "+truckTicketCount);
System.out.println("Total tickets sold: "+totalTicketCount);
}
if (userInput == 0)
{
running = false; //terminates the program
}
}//end of else after the if (hasNextInt())
}//end of first if (hasNextInt())
}//end of try
catch (NumberFormatException e) //this SHOULD catch an invalid input error
{
System.out.println("Invalid input."); //displays invalid input in case the
userInput = -1;
}
}
}
}
在工作中,如果我给你的东西超级破碎,因为我现在无法编译和测试。但这应该给你正确的想法。