我正在制作订购系统。如果没有为数量输入值,我需要它抛出错误。我是一名初学者,但仍然坚持使用IDE而不是学习基础知识。这是我的代码:
/**
* Course: IT110 - Introduction to Programming
* Filename: NMPTireShopPhase1.java
* Created: 04/09/10 by Dr. Debby Telfer
* Modified: 11/26/13 by Dr. Bary W Pollack
* Modified: 10/14/14 by Michael Hodges
* Purpose: Created a simple online ordering system
* for NMP Tire Shop customers
*/
import javax.swing.JOptionPane;
import java.sql.Statement;
import java.util.InputMismatchException;
/**
* @author bary
* @author Mike
*/
class NMPTireShopPhase1 {
private static final boolean InputMismatchException = false;
private static final boolean AssignmentOperatorExpression = false;
/**
* @param args
*/
public static void main(String[] args) {
// declare variables
String openingMsg, nameInputMsg, customerName, nameOutputMsg,
returnInputMsg, customerReturn, returnOutputMsg,
typeInputMsg, typeReturn, typeOutputMsg,
quantityInputMsg, quantityReturn, quantityOutputMsg,
greetingOutputMsg, outputMsg;
// display opening message
openingMsg = "*** Welcome to NMP Tire Shop Online Ordering System ***\n"
+ " Allow us to fix your tires!";
JOptionPane.showMessageDialog(null, openingMsg);
// get required input using dialogs
nameInputMsg = "Please enter your name: ";
String prompt = JOptionPane.showInputDialog(nameInputMsg );
customerName = getStringInput11(prompt );
returnInputMsg = "please state what vehichle your tire is for: ";
String prompt1 = JOptionPane.showInputDialog(returnInputMsg);
customerReturn = getStringInput11(prompt1 );
typeInputMsg = "please state the type of rim: ";
String prompt2 = JOptionPane.showInputDialog(typeInputMsg);
typeReturn = getStringInput11(prompt2);
quantityInputMsg = "how many tires do you want repaired?(1-99)";
String prompt3 = JOptionPane.showInputDialog(quantityInputMsg);
System.out.println("enter your quantity");
System.out.println(quantityInputMsg);
quantityReturn = getStringInput11(prompt3);
// build output strings
nameOutputMsg = "Welcome " + customerName + ".\n\n";
returnOutputMsg = " Your tire is for a " + customerReturn + ".\n\n" ;
typeOutputMsg = " Your rim type is " + typeReturn + ".\n\n" ;
quantityOutputMsg = " You want this many tires repaired " + quantityReturn +".\n\n";
greetingOutputMsg = " Thank you for visiting NMP Tire Shop!" + "\n\n"
+ "Your tires should be returned in less than a month.\n";
// create and display output string
outputMsg = (nameOutputMsg + returnOutputMsg + typeOutputMsg + quantityOutputMsg + greetingOutputMsg);
JOptionPane.showMessageDialog(null, outputMsg);
System.exit(0);
} // end main()
{
catch(InputMismatchException) Statement AssignmentOperatorExpression; (InputMismatchException);
{
System.out.println("pleas enter numerical value only.");
System.exit(3);}
}
private static String getStringInput11(String quantityInputMsg) {
// TODO Auto-generated method stub
return quantityInputMsg;
}
private void AssignmentOperatorExpression(boolean inputmismatchexception2) {
// TODO Auto-generated method stub
}
}
// end class NMPTireShopPhase1
我不知道捕获是否在错误的地方,但它甚至不会运行。我需要它来捕获错误并在终止程序之前允许三次尝试。这是正确的方法吗?我只是错过了一个命令,还是在完全错误的地方?
答案 0 :(得分:0)
catch
语法应该类似于
} catch (InputMismatchException e) {
System.out.println("pleas enter numerical value only.");
e.printStackTrace(); // <-- Please don't swallow the exception.
System.exit(3);
}
如果您想使用multi-catch,可能会使用类似
的内容} catch (IOException|InputMismatchException e) {
System.out.println("pleas enter numerical value only.");
e.printStackTrace(); // <-- Please don't swallow the exception.
System.exit(3);
}