2016-04-03 28 views
-2

我有一個小問題,我需要幫助。異常處理程序 - 打印消息和單個println中的變量行

對於這種分配(第12課HW9)名稱NumberFormatException的,我不得不做以下

下面的「Calculator.java,是一個簡單的命令行計算器。請注意,如果任何操作數爲非數字程序將終止。用一個處理非數字操作數的異常處理程序編寫程序,程序應該顯示一條消息,告知用戶在退出前錯誤的操作數(見下圖)。「

c:\exercise>java Exercise12_01 "4 + 5" 

4 + 5 = 9 

c:\exercise>java Exercise12_01 "4 - 5" 

4 - 5 = -1 

c:\exercise>java Exercise12_01 "4x - 5" 

輸入錯誤:4倍 SO編碼的所有數學部分完成後,正確的,而且作品中,我似乎無法找出的唯一部分是:「顯示通知錯誤的用戶信息在退出之前的操作數錯誤輸入:4x。我需要知道我可以做什麼來顯示不僅錯誤信息,而且還顯示錯誤輸入。它向用戶顯示錯誤信息的部分位於代碼末尾

另外還有一點我忘了提及。問題是用戶可能只會得到「N」或「V」錯誤,我不知道如何只將變量放在錯誤的位置,而只留下另一個變量 EX:如果用戶放4x - 5(這是N - V),我只是想發佈4倍(N),而不是5(V),這樣我就可以向用戶顯示什麼輸入錯了

catch(InputMismatchException ex) 
{ 
    System.out.println("Bad input, please correct your operard."); 
} //need to add the input error as well 

這裏的全部代碼,謝謝

import java.util.InputMismatchException; 
import java.util.Scanner; 

public class NumberFormatException { 

    public static void main(String[] args) 

    { 
     Scanner Read = new Scanner(System.in); 
     try{ 
//   System.out.println("What is your number?"); 
      System.out.println("please enter an equation"); 
      int n=Read.nextInt(); 
      char operands; 
      operands=Read.next().charAt(0); //reading up till the next space and pulling the first character it read 
//   System.out.println("What is your operand?"); 
//   operands=Read.next().charAt(0); 
//  
//    
//   System.out.println("What is a second number?"); 
      int v=Read.nextInt(); 
//   System.out.println("The second number you entered was "+v); 
      if (operands == '+') 
       System.out.println(n+"+"+v+"="+addition(n, v)); 
      if (operands == '-') 
       System.out.println(n+"-"+v+"="+subtraction(n, v)); 
      if (operands == '*') 
       System.out.println(n+"*"+v+"="+multiply(n, v)); 
      if (operands == '/') 
       System.out.println(n+"/"+v+"="+division(n, v)); 

     } 
     catch(InputMismatchException ex) 
     { 
      System.out.println("Bad input, please correct your operard."); // 
     } 
    } 


     public static int addition(int n, int v){ 
     return n+v; 
    } 
    public static int subtraction(int n, int v){ 
     return n-v; 
    } 
    public static int multiply(int n, int v){ 
     return n * v; 
    } 
    public static int division(int n, int v){ 
     return n/v; 
    } 

} 

如果你想>可以複製併發布到eclipse中。 <感謝


PS:如果你認爲這個標題和問題,你有似曾相識的感覺你不是,我不小心貼在底部的錯誤代碼與以前的帖子,關於球員,但這裏的正確的代碼對不起

PS2:好的用戶在底部用他的代碼版本回答了這個問題,但還有另一個問題。

有一個問題在做,是否有一種方法來顯示輸入。我試圖顯示4倍作爲錯誤,以及您修改的打印輸出,但它不出來

System.out.println(「輸入錯誤,請更正您的第一個操作數。」+ n);

我剛剛嘗試過,但它要我聲明n = 0所以當我這樣做,但然後:「輸入錯誤,請更正您的第一個操作數0」它把一個零而不是4x我該如何修復這是一個字符串轉換?請和謝謝

+0

你只需要2 try/catch塊,一個圍繞N = Read.nextInt()和一個圍繞V = Read.nextInt(),或得到的字符串,你以後轉換。 – Turo

+1

你可以編輯你以前的問題,而不是重新註冊並創建一個新的問題,但無論如何...閱讀問題定義後,我認爲您不必要求用戶輸入數據。您應該直接使用main方法中的args [0]。您必須解析FirstOperand [blank] Operator [blank] SecondOperand格式的字符串。如果FirstOperand不是int,則爲錯誤。如果SecondOperand不是int,則爲錯誤。如果操作員沒有定義,錯誤。否則做手術。 – RubioRic

+0

感謝您的建議和提示,抱歉重新發布它,即時通訊新的stockflow並花了我一段時間才知道您每天只能發佈1個問題或進行編輯。我無法刪除它,因爲它的答案很快,但現在我知道 – Mark1247

回答

0

有解析輸入和檢查有效性的更好的方法。但是,使用你的代碼作爲基礎,這是你可以做的工作。爲每個整數添加一個額外的try/catch子句。

public static void main(String[] args) { 
    Scanner Read = new Scanner(System.in); 
    int n; 
    int v; 
    System.out.println("Please enter an equation"); 
    try { 
     n = Read.nextInt(); 
    } catch (InputMismatchException ex) { 
     System.out.println("Bad input, please correct your first operand."); 
     return; 
    } 
    char operands; 
    operands = Read.next().charAt(0); //reading up till the next space and pulling the first character it read 
    try { 
     v = Read.nextInt(); 
    } catch (InputMismatchException ex) { 
     System.out.println("Bad input, please correct your second operand."); 
     return; 
    } 
    if (operands == '+') 
     System.out.println(n + "+" + v + "=" + addition(n, v)); 
    if (operands == '-') 
     System.out.println(n + "-" + v + "=" + subtraction(n, v)); 
    if (operands == '*') 
     System.out.println(n + "*" + v + "=" + multiply(n, v)); 
    if (operands == '/') 
     System.out.println(n + "/" + v + "=" + division(n, v)); 

} 


public static int addition(int n, int v) { 
    return n + v; 
} 

public static int subtraction(int n, int v) { 
    return n - v; 
} 

public static int multiply(int n, int v) { 
    return n * v; 
} 

public static int division(int n, int v) { 
    return n/v; 
} 

}

+0

哦,我的上帝,非常感謝你,哇這樣一個簡單的方法來解決這個,謝謝 但有一個問題在做,有沒有辦法顯示輸入以及 我試圖顯示4x作爲錯誤以及打印輸出您修改,但它並沒有出來 System.out.println(「輸入錯誤,請更正您的第一個操作數。」+ n); 我想,剛纔,但它要我申報N = 0 所以,當我這樣做,但後來: 「壞輸入,請糾正你的第一operand.0」 它放在一個零,而不是4倍 哪有我解決這個問題,一個字符串轉換?謝謝, – Mark1247

+0

那麼,因爲引發異常的原因是,Read.nextInt()失敗的操作數與錯誤的格式是永遠不會被實際讀取,因此它仍然是0.你可以做的是在catch子句,閱讀將失敗的輸入作爲字符串並打印出來。 – Ozgar

+0

'System.out.println(「Bad input,correct」+ Read.next()+「and try again。」)' 應該工作。如果這有幫助,請標記爲已解決。 – Ozgar