2013-07-24 64 views
0

我遇到了一些與我交給java計算器任務有關的問題。我被告知要製作一個非常基本功能的計算器,捕捉異常並允許您立即糾正操作數或操作符的值(這是我遇到的問題)。舉例來說,這是在控制檯應該發生什麼:java計算器(重新輸入值)

j * 6 
Catch exception and print error message here and asking for new first operand 
4 
Answer: 4 * 6 = 24 

8 h 9 
Catch exception and print error message here asking for new operator 
+ 
Answer: 8 + 9 = 17 

這段代碼是我到目前爲止有:

import java.util.*; 
public class Calculator{ 

static int _state = 3; 

public static void main(String[] args){ 

_state = 3; 

System.out.println("Usage: operand1 operator operand2"); 
System.out.println(" (operands are integers)"); 
System.out.println(" (operators: + - * /"); 
@SuppressWarnings("resource") 
Scanner in = new Scanner(System.in); 
do{ 

try{ 
int result = 0; 
int operand1 = 0; 
int operand2 = 0; 

String operator = ""; 
char op = ' '; 

operand1 = in.nextInt(); 

operator = in.next(); 
op = operator.charAt(0); 
operand2 = in.nextInt(); 

switch (op){ 

    default: 
     System.out.println("You didn't insert a proper operator"); 
     break; 
    case '+': result = operand1 + operand2; 
     System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result); 
     break; 

    case '-': result = operand1 - operand2; 
     System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result); 
     break; 

    case '*': result = operand1 * operand2; 
     System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result); 
     break; 

    case '/': result = operand1/operand2; 
     System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result); 
     break; 
     } 

} 
    catch(ArithmeticException e){ 
     System.out.println("You can not divide by zero. Input a valid divider."); 
    } 
    catch (InputMismatchException e) { 
     System.out.println("You must use proper numerals."); 
    } 
} while(_state == 3); 

} 
} 
+4

有什麼問題....? –

+0

我提供了這個控制檯的示例,您可以立即重新輸入以前的無效值,我需要關於如何爲計算器編寫代碼的想法/示例。 –

回答

0

你需要做的幾件事在完成之前。我建議通過擴展Exception類(或Exception類的子類)來創建自己的Exceptions。您可以通過閱讀下面的做到這一點:

How to create custom exceptions in Java?

現在說你創建MissingOperatorException,那麼您可以在交換機/ Case語句扔掉它。

try{ 
    switch (op){ 

     default: 
      System.out.println("You didn't insert a proper operator"); 
      throw MissingOperatorException; 
      break; 
     //some more code here 
    } 
} 
//reach catch statements 
catch(MissingOperatorException e){ 
    System.out.println("ERROR MESSAGE"); 
    //deal with case here by asking for new input 
    //use scanner to get operand 
    //you can do this by reusing code from above 
    System.out.println("Please re-enter operand"); 
    operator = in.next(); 
    op = operator.charAt(0); 
    //calculate answer again 
    //print out answer 
} 

您需要爲每種需要執行的異常執行此操作。我看到三種可能的情況。

  1. 缺少操作員
  2. 缺少左操作
  3. 缺少右操作數

有可能是這些問題的一個組合,但異常處理你寫的應該能夠照顧它作爲它(一個接一個地)。

注意:您已使用Scanner類讀入用戶輸入。我會假設你瞭解Scanner的基本知識,但你的後續問題與此相矛盾。我真的會研究Java Scanner類。請檢查了這一點:

http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

+0

你說「//使用掃描儀獲取操作數」的部分,我該怎麼做?這是我頭部旋轉數週的部分。 –

+0

剛剛更新了我的帖子。您真的需要了解Scanner類的工作原理。 –

+0

好的,我想我已經對重新輸入的值有一點了解。我會玩,看看我得到了什麼。感謝您的幫助。 –

0

它被認爲是不好的做法,有一個巨大的try塊捕獲的一切。把try catch塊放在你需要的地方。

此外,人們並不喜歡爲他們做別人的功課。如果你有一個特定的問題或問題,那很好,但是說「我必須爲這個任務做這件事,我該如何做所有事情」可能不會飛。

+0

第一部分:注意。第二部分:110%不是這種情況。 –

0

你應該試試這個

import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     boolean ret = true; 
     while(ret){ 

     Scanner scn = new Scanner(System.in); 
     String answer = ""; 
     String answer2 = ""; 
     System.out.println("Welcome to java calculator....."); 
     System.out.print("\nEnter the first number: "); 
     double numA = scn.nextDouble(); 
     System.out.print("Enter the second number: "); 
     double numB = scn.nextDouble(); 
     System.out.println("What you want to calculate?"); 
     double result = 0; 

     answer = scn.next(); 

     if(answer.equals("+")){ 
      result = numA + numB; 
     } 

     if(answer.equals("-")){ 
      result = numA - numB; 
     } 

     if(answer.equals("*")){ 
      result = numA * numB; 
     } 

     if(answer.equals("/")){ 
      result = numA/numB; 
     } 

     System.out.println("The result is: " + result); 

     System.out.println("Do you want to continue?"); 
     answer2 = scn.next(); 

     if(answer2.equalsIgnoreCase("y")){ 
      ret = true; 
      } 

     else{ 
      System.out.println("Good bye....:)"); 
      System.exit(0); 
      } 
     } 
    } 
}