2013-10-25 36 views
0

我想爲Android做一個簡單的計算器應用程序,我現在卡住了。我打算製作2個計算器(一個基本,另一個更高級,與Windows中構建的類似)。這個想法是讓它在3個不同的類。java中的計算器錯誤(類)

兩個類將包含「主」代碼(按鈕,佈局),最後一個應包含我打算調用的函數(如add,multiply)。我設法完成了一個類中的兩個計算器,但我不知道如何讓他們從其他類調用函數。

問題:我設法創建了基本的計算器,它可以調用另一個類的函數,但是如果我想一個接一個地添加更多的計算,它將無法正常工作。我的意思是這樣的:假設我的第一個數字是1,第二個數字是3.它打印結果4,但是如果我再次點擊添加,並將2打印結果5而不是6.不知何故,它將第二個變量存儲到第一個,我不能提前

enter code here 


package com.example.simcalc; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 
    import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainInterface extends Activity implements OnClickListener { 
    static EditText disp; 
    static TextView txt1, txt2; 
    SimFunctions sm; 
    TextView tx1; 
    Button btDot, bt1, bt2, bt3, bt4, bt5, bt6, bt7 ,bt8, bt9, bt0, btPlus, btMinus, btDivide, btMult,btEquals, btC; 
     float num2 =0 , res; 
     float num1 = 0; 
    String saveNumber = ""; 
    static char sim ; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_interface); 
     disp = (EditText) findViewById(R.id.enterNumber); 
     bt1 = (Button) findViewById(R.id.one); 
     bt2 = (Button) findViewById(R.id.two); 
     bt3 = (Button) findViewById(R.id.three); 
     bt4 = (Button) findViewById(R.id.four); 
     bt5 = (Button) findViewById(R.id.five); 
     bt6 = (Button) findViewById(R.id.six); 
     bt7 = (Button) findViewById(R.id.seven); 
     bt8 = (Button) findViewById(R.id.eight); 
     bt9 = (Button) findViewById(R.id.nine); 
     bt0 = (Button) findViewById(R.id.zero); 
     btPlus = (Button) findViewById(R.id.add); 
     btMinus = (Button) findViewById(R.id.sub); 
     btMult = (Button) findViewById(R.id.mult); 
     btDivide = (Button) findViewById(R.id.div); 
     btC = (Button) findViewById (R.id.can); 
     btDot = (Button) findViewById(R.id.decimal); 
     btEquals = (Button) findViewById(R.id.equal); 
     txt1 = (TextView) findViewById(R.id.textView1); 
     txt2 = (TextView) findViewById(R.id.textView2); 
     bt1.setOnClickListener(this); 
     bt2.setOnClickListener(this); 
     bt3.setOnClickListener(this); 
     bt4.setOnClickListener(this); 
     bt5.setOnClickListener(this); 
     bt6.setOnClickListener(this); 
     bt7.setOnClickListener(this); 
     bt8.setOnClickListener(this); 
     bt9.setOnClickListener(this); 
     bt0.setOnClickListener(this); 
     btPlus.setOnClickListener(this); 
     btMinus.setOnClickListener(this); 
     btDivide.setOnClickListener(this); 
     btMult.setOnClickListener(this); 
     btC.setOnClickListener(this); 
     btDot.setOnClickListener(this); 
     btEquals.setOnClickListener(this); 
     disp.setText("0"); 
     sm = new SimFunctions(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main_interface, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch (v.getId()){ 
     case R.id.one: 
      saveNumber += "1"; 
      disp.setText(saveNumber); 
      break; 

     case R.id.two: 
      saveNumber += "2"; 
      disp.setText(saveNumber); 
      break; 

     case R.id.three: 
      saveNumber += "3"; 
      disp.setText(saveNumber); 
      break; 

     case R.id.four: 
      saveNumber += "4"; 
      disp.setText(saveNumber); 
      break; 

     case R.id.five: 
      saveNumber += "5"; 
      disp.setText(saveNumber); 
      break; 

     case R.id.six: 
      saveNumber += "6"; 
      disp.setText(saveNumber); 
      break; 
     case R.id.seven: 
      saveNumber += "7"; 
      disp.setText(saveNumber); 
      break; 

     case R.id.eight: 
      saveNumber += "8"; 
      disp.setText(saveNumber); 
      break; 

     case R.id.nine: 
      saveNumber += "9"; 
      disp.setText(saveNumber); 
      break; 

     case R.id.zero: 
      saveNumber += "0"; 
      disp.setText(saveNumber); 
      break; 


     case R.id.decimal: 
      if (saveNumber.contains(".")){ 
       break; 
      } 
      else{ 
      saveNumber += "."; 
      disp.setText(saveNumber); 
      break; 
      } 
     case R.id.mult: 
      sim = '*'; 
      if (saveNumber != ""){ 
      num1 = Float.parseFloat(saveNumber); 
      saveNumber = ""; 

      break;} 
      else{ 

      break;} 

     case R.id.add: 
      sim = '+'; 
      if (saveNumber != ""){ 
      num1 = Float.parseFloat(saveNumber); 
      saveNumber = ""; 

      break;} 
      else{ 
      break;} 

     case R.id.div: 
      sim = '/'; 
      if (saveNumber != ""){ 
      num1 = Float.parseFloat(saveNumber); 
      saveNumber = ""; 

      break;} 
      else{ 
      break;} 

     case R.id.sub: 
      sim = '-'; 
      if (saveNumber != ""){ 
      num1 = Float.parseFloat(saveNumber); 
      saveNumber = ""; 

      break;} 
      else{ 
      break;} 


     case R.id.equal: 
      sm.equals(num1, saveNumber, sim); 
      break; 

     case R.id.can: 
      saveNumber = ""; 
      disp.setText(saveNumber); 
      break; 

     } 
    } 



} 

    package com.example.simcalc; 


import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class SimFunctions extends MainInterface { 




    public void equals(float num1, String saveNumber, char simbol){ 

     String ds = ""; 
     if (saveNumber != "") { 
      try{ 
       num2 = Float.parseFloat(saveNumber);} 

     catch(NumberFormatException e){ 
       e.printStackTrace(); 
     } 
     } 
     else { 
     num2 = 0;} 
     switch (simbol){ 
     case ('*'): 
      res = num1 * num2; 
      num1 = res; 
      ds = Float.toString(res); 
      disp.setText(ds); 
      saveNumber = ""; 
      ds = ""; 
      break; 

     case ('+'): 
      ds = Float.toString(num1); 
      txt1.setText(ds); 
      ds = Float.toString(num2); 
      txt2.setText(ds); 
      res = num1 + num2; 
      num1 = res; 
      ds = Float.toString(res); 
      disp.setText(ds); 
      saveNumber = ""; 

      ds = ""; 
      break; 

     case ('/'): 
      res = num1/num2; 
      num1 = res; 
      ds = Float.toString(res); 
      disp.setText(ds); 
      saveNumber = "";`enter code here` 
      ds = ""; 
      break; 


    enter code here 
     case ('-'): 
      res = num1 - num2; 
      num1 = res; 
      ds = Float.toString(res); 
      disp.setText(ds); 
      saveNumber = ""; 
      ds = ""; 
      break; 
     } 

    } 



} 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/testBlack" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainInterface" > 

    <EditText 
     android:id="@+id/enterNumber" 
     android:layout_width="match_parent" 
     android:layout_height="110dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:background="@color/scoreColor" 
     android:inputType="numberDecimal" /> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/linearLayout2" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="86dp" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:paddingTop="20dp" > 

     <Button 
      android:id="@+id/seven" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="7" /> 

     <Button 
      android:id="@+id/eight" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="8" /> 

     <Button 
      android:id="@+id/nine" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="9" /> 

     <Button 
      android:id="@+id/div" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="/" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/linearLayout1" 
     android:layout_alignLeft="@+id/enterNumber" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:paddingTop="20dp" > 

     <Button 
      android:id="@+id/four" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="4" /> 

     <Button 
      android:id="@+id/five" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="5" /> 

     <Button 
      android:id="@+id/six" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="6" /> 

     <Button 
      android:id="@+id/mult" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="*" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/linearLayout2" 
     android:layout_below="@+id/enterNumber" 
     android:layout_marginTop="20dp" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:paddingTop="20dp" > 

     <Button 
      android:id="@+id/one" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="1" /> 

     <Button 
      android:id="@+id/two" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="2" /> 

     <Button 
      android:id="@+id/three" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="3" /> 

     <Button 
      android:id="@+id/sub" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="-" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/linearLayout1" 
     android:layout_alignParentBottom="true" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:paddingTop="20dp" > 

     <Button 
      android:id="@+id/can" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="c" /> 

     <Button 
      android:id="@+id/zero" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="0" /> 

     <Button 
      android:id="@+id/equal" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="=" /> 

     <Button 
      android:id="@+id/add" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="+" /> 

     <Button 
      android:id="@+id/decimal" 
      android:layout_width="55dp" 
      android:layout_height="wrap_content" 
      android:text="." /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/linearLayout3" 
     android:layout_alignParentLeft="true" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/linearLayout3" 
     android:layout_toRightOf="@+id/textView1" 
     android:text="TextView" /> 

</RelativeLayout> 

回答

0

你把操作的結果爲全局變量按下一次「NUM1」。 「找出原因:( 誰能幫我? 謝謝+'你用前面的 saveNumber值覆蓋那個值。

所以你的邏輯是這樣的:

1 -> savenumber = 1 
+ -> num1 = 1 
3 -> savenumber = 3 
= -> num2 = savenumber 
    -> res = num1 + num2 
    -> num1 = res 
+ -> num1 = savenumber = 3 
2 -> savenumber = 2 
= -> num2 = savenumber 
    -> res = num1 + num2 
      = 3 + 2 
+0

TY爲你回覆。我明白這樣的事情一定是個問題,但我完全不能理解它。我的意思是我把saveNumber =「」我已經計算了num1 + num2之後,爲什麼它仍然將saveNumber視爲它的值爲3?我很笨,我不知道如何解決這個問題... – Proxy

+0

呃我設法解決它在最後。我將等價函數的參數從String saveNumber更改爲了sNumber,因爲我剛纔明白了在我引用局部變量saveNumber而不是全局變量之前。此外,我不得不將全局saveNumber更改爲靜態saveNumber – Proxy