2013-12-15 68 views
0

我正在試圖製作一個計算器,用戶在一行輸入數據,然後按「=」計算結果。但是,我不知道如何輸入輸入的數據進行計算。 1)我是否接受整個字符串並計算它(這意味着android必須能夠區分諸如+和 - 的符號)2)或者當用戶輸入數字時我會逐個接收數據。 到目前爲止,我有這樣的:如何在一個android計算器中計算結果

public void onClickButton (View view) 
    { 
     if (view.getId() == (R.id.Button01)){ 
      txtView1.append("7"); 
     } else if (view == findViewById(R.id.Button02)){ 
      txtView1.append("8"); 
     } else if (view == findViewById(R.id.Button03)){ 
      txtView1.append("9"); 
     } else if (view == findViewById(R.id.Button04)){ 
      txtView1.append("/"); 
     } else if (view == findViewById(R.id.Button05)){ 
      txtView1.append("4"); 
     } else if (view == findViewById(R.id.Button06)){ 
      txtView1.append("5"); 
     } else if (view == findViewById(R.id.Button07)){ 
      txtView1.append("6"); 
     } else if (view == findViewById(R.id.Button08)){ 
      txtView1.append("+"); 
     } else if (view == findViewById(R.id.Button09)){ 
      txtView1.append("1"); 
     } else if (view == findViewById(R.id.Button10)){ 
      txtView1.append("2"); 
     } else if (view == findViewById(R.id.Button11)){ 
      txtView1.append("3"); 
     } else if (view == findViewById(R.id.Button12)){ 
      txtView1.append("-"); 
     } 
    } 

這得到了文本在一個字符串 如出來:「5 + 45」

+0

你應該做一個變量來存儲第一個數字,如果他們按+, - ,*,或/和四個布爾每按一下+, - ,*,或/和適當的一個是真實的,然後當他們按'='做適當的計算與收集的所有信息 – JRowan

+0

這隻會適用於2個數字,雖然正確嗎? – user2883071

+0

每次你選擇一個運營商商店的結果在變量,這就是所有,那麼你可以做任意多的數字,只要你想 – JRowan

回答