0

我正在尝试为 android 制作一个简单的计算器应用程序,但我现在被卡住了。我打算制作 2 个计算器(一个基本的,另一个更高级的,类似于 Windows 内置的)。这个想法是把它分成3个不同的班级。

两个类将包含“主”代码(按钮、布局),最后一个类应包含我计划调用的函数(如加法、乘法)。我设法在一堂课上完成了两个计算器,但我不知道如何让它们调用另一堂课的函数。

问题:我设法创建了基本计算器,它可以调用其他类的函数,但是如果我想一个接一个地添加更多计算,它就无法正常工作。我的意思是:假设我的第一个数字是 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>
4

1 回答 1

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
于 2013-10-25T10:44:27.230 回答