2011-03-22 75 views
0

我需要製作一個簡單的計算器。我無法輸入任何數量的運算符和操作數,然後在輸入等號按鈕時輸出答案。製作一個簡單的計算器:不能退出循環或給出答案

到目前爲止,如果我只是按一個數字和一個數字它退出但沒有給我一個答案。如果我做了多個操作符和操作數,然後=它不會退出循環。 例如,它應該是這樣的:

5 
+ 
5 
+ 
5 
= 
15 

這裏是我的代碼,計算器:

public interface Calculator { 
    public void setOperator(char operator);   // eg +-*/= 
    public void setOperand (double operand);  // eg 123.456 
    public double getResult(); 
} 

SimpleCalculator:

import java.io.*; 

public class SimpleCalculator implements Calculator { 
    char operator; 
    double operand; 
    double result; 
    double answer; 

    public void setOperator(char operator){ 
     this.operator = operator; 
    } 

    public char getOperator(){ 
     return operator; 
    } 

    public void setOperand(double operand){ 
     this.operand = operand; 
    } 
    public double getOperand(){ 
     return operand; 
    } 

    public double getResult(){ 
     if (getOperator() == '+'){ 
      result = (getOperand() + getOperand()); 
     } 
     if (getOperator() == '-'){ 
      result = (getOperand() - getOperand()); 
     } 
     if (getOperator() == '*'){ 
      result = (getOperand() * getOperand()); 
     } 
     if (getOperator() == '/') 
     { 
      result = (getOperand()/getOperand()); 
     } 
     if (getOperator() == '=') 
      result = answer; 
    } 

    return result; 
} 

public boolean getanswer(String value) 
{ 
    boolean isnum = false; 
    try { 
     setOperand(Double.parseDouble(value)); 
     operand = (Double.parseDouble(value)); 
     getResult(); 
     isnum = true; 

    } 
    catch(Exception e) 
    { 
     try { 
      setOperator(value.charAt(0)); 
      operator = (value.charAt(0)); 
      isnum = false; 
     } 
     catch(Exception e2) 
     { 

      { 
       System.out.println("Enter a number"); 
      } 
     } 
     return isnum; 
    } 
} 

SimpleTest的:基於

import java.io.*; 

public class SimpleTest{ 
    static String value; 
    static double operand; 
    static char operator; 
    static boolean isnum; 

    public static void main(String[] argv){ 
     SimpleCalculator calculator = new SimpleCalculator(); 
     value = UserInput.readString(); 
     while (!(value.equals("="))) 
     { 
      isnum = calculator.getanswer(value); 
      if (!(isnum == true)) 
      { 
       break; 
      } 
     } 
     System.out.println(calculator.getResult()); 
    } 

} 
+0

請更具體地說明您遇到的問題。你希望我們只是爲你解決你的計劃嗎? – Blorgbeard 2011-03-22 19:43:41

+0

我很抱歉地說你的代碼設計不好。你自己寫了一切嗎?繼續努力吧,畢竟它是作業。 – Bernard 2011-03-22 19:55:07

回答

1

山雀你的問題的文件,我發現你可能會看到一個問題,你的主循環:

value = UserInput.readString(); 
while (!(value.equals("="))) { 
    isnum = calculator.getanswer(value); 
    if (!(isnum == true)) { 
     break; 
    } 
} 

既然你讀外循環用戶輸入它永遠不會改變,這將要麼只運行一次(如果ISNUM是假的)或無限(如果isnum爲真) - getanswer在結果方面沒有記憶。因此,如果你輸入一個數字,它將永遠循環,但沒有做任何有用的事情。

請注意:這只是第一次猜測。我沒有檢查你的程序的其餘部分。

0

你真的不說你的問題是什麼,但我找到一個適合你:

您正在使用

result = (getOperand() + getOperand(); 

(以及類似)來計算你的結果。但是getOperand()總是返回相同的結果(因爲您不能在這些調用之間執行setOperand()),所以您總是加,減,乘和除相同的數字。

+0

我將如何去改變這? – Ahsan 2011-03-22 20:52:22

+0

你必須先記住當前和操作數,然後有兩個getter方法。 – 2011-03-22 23:03:58