上午建立書店系統 其中一種方法會要求用戶輸入書名然後說明書的代碼。 我的問題是,我怎麼能回想起來,如果有「InputMismatchException」。 我的意思是捕獲將打印到secreen「錯誤的輸入!」但我想提示用戶重新輸入使用try-catch,如果輸入錯誤,我該如何再次回想一遍?
這裏是我的代碼
public void add(){
try{
System.out.println("Enter the book code :");
int c = s.nextInt();
try{
System.out.println("Eneter the book quantity");
int q = s.nextInt();
try{
System.out.println("Eneter the book description");
String d = s.next();
try{
System.out.println("Eneter the book cost price");
double cp = s.nextDouble();
try{
System.out.println("Eneter the book selling price");
double sp = s.nextDouble();
try{
System.out.println("Enter status: [a for available - u for unvailable]");
String input = s.next();
if(input.equals("a")) {
System.out.println("available: ");
}
else if (input.equals("u")) {
System.out.println("unavailable ");
}
else {
System.out.println("Wrong Input");
}
try{
System.out.println("enter the discount on the item \"if exsist\"");
double dis = s.nextDouble();
}catch(InputMismatchException e){
System.out.println("Wrong Input");
}
}catch(InputMismatchException e){
System.out.println("Wrong input");
}
}catch(InputMismatchException e){
}
}catch(InputMismatchException e){
System.out.println("WRONT INPUT!!");
}
}catch(InputMismatchException e){
System.out.println("WRONG INPUT!!");
}
} catch(InputMismatchException e){
System.out.println("Wrong input. NUMBERS only!!");
}
} catch(InputMismatchException e){
System.out.println("You have to enter a NUMBER only");
}
這些爲什麼嵌套? – 2014-10-12 04:06:04
你正在提示的每件作品都應該是它自己的一種方法。在該方法內部,處理循環直到獲得有效的輸入(或退出標誌)。當方法返回時,它將返回有效的輸入(可能需要幾次嘗試)或返回結束輸入的標誌(可能返回'null')。 – Jared 2014-10-12 04:06:07
HOLY COW GUACAMOLE – Tdorno 2014-10-12 04:15:59