2011-03-29 90 views
0

基本上,我想打一個簡單的計算器添加一個字符串到另一個字符串轉向雙或字符,然後計算結果

我想借此從鍵盤的字符串輸入,因此如果一個號碼或」。 「它會將它添加到字符串,如果它是一個char(+ - * /)它將執行計算。

所以,你先輸入3

然後4,這將使得串34,然後將其轉換爲雙然後做一個+,然後再輸入3。那麼34這樣的字符串是3.34然後你按=,它給出了答案。 我已讀入值 然後

string newstring = value; 
if (value.equals("0") |value.equals("1") etc 
in = in + newstring; 
} 

然後

try 
{ 
setOperand(convert in to double) 
if (flag == 1) 
{ 
operand1 = convert in to double 
result = operand1 
flag = 0; 
}// only does it for the first operand 

getresult() which is if + then result + getOperand 
catch 
try 
changes to char 

由於某種原因,當我只是做3 =結果= 0,或3然後。然後2等

+0

你能張貼代碼和格式它有點難以分辨究竟是怎麼回事 – Merky 2011-03-29 16:36:04

+0

哇...請閱讀剛纔發佈的問題,並決定是否反映了您希望幫助我們的努力,如果您選擇將其編輯爲把它帶到質量值得回答的地方,你可以使用問題下方的[編輯鏈接](http://stackoverflow.com/posts/5475829/edit)。 – 2011-03-29 16:37:02

+0

你一遍又一遍地問同樣的問題 - 不要你認爲是閱讀並嘗試理解答案的時候了嗎? – 2011-03-29 17:35:17

回答

1

我可以提出建議嗎?獲取整個字符串在一行中計算,然後解析該行。

import java.util.Scanner; 
public class AshanCalc { 

    public static void main(String[] args) { 
     System.out.println("Welcome to my calculator. Please enter a math equation:"); 

     Scanner in = new Scanner(System.in); 
     String equation = in.nextLine(); // get the ENTIRE line from the keyboard 
             // not just one character at a time 

     double result = evaluateEquation(equation); 

    } 

    private static double evaluateEquation(String equation) { 
     /** 
     * your mission - fill this in 
     */ 
    } 
} 
0

還可以使用字符的方法ISDIGIT()而不是使用if(value.equals( 「0)|| value.equals(」 1" )等。

相關問題