Android的简单计算器应用程序

时间:2015-12-04 19:15:51

标签: android

这个简单的计算器应用程序有一个奇怪的问题。 添加/减去/乘法和除法工作,但是: 1.当数字乘以十进制值(一切都是双打)时,我的结果乘以10。 例如:7 * 7给我49.0 2.当乘以数字时,我有时得到一个近乎好的结果(只乘以10,但是后面跟着很多零,最后是1) 示例:1.1 * 1.1 = 12.100000000000001

package org.example.paweljezierski.calculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //I'm declaring every button so I'm able to use it
    //I could probably do it in one line, but it's cleaner this way
    Button buttonplus;
    Button buttonminus;
    Button buttonmultiply;
    Button buttonpower;
    Button buttondivide;
    Button buttonequals;
    Button buttondot;
    Button buttonclear;
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;
    Button button6;
    Button button7;
    Button button8;
    Button button9;
    Button button0;

    //I'm declaring every TextView
    TextView textresult;
    TextView textinput;

    //value1, value2 are two values that we'll do math on
    double value1 = 0, value2 = 0, result = 0;
    //mathoperation will be set to [1;4] by operation button to know what math to do later
    int mathoperation = 0;
    //for holding information
    String a, a2, aa, n;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Initialize both text fields
        textresult = (TextView)findViewById(R.id.textresult);
        textinput = (TextView)findViewById(R.id.textinput);

        //I initialize every button
        buttonplus =(Button)findViewById(R.id.buttonplus);
        buttonminus =(Button)findViewById(R.id.buttonminus);
        buttonmultiply =(Button)findViewById(R.id.buttonmultiply);
        buttondivide =(Button)findViewById(R.id.buttondivide);
        buttonpower =(Button)findViewById(R.id.buttonpower);
        buttondivide =(Button)findViewById(R.id.buttondivide);
        buttonequals =(Button)findViewById(R.id.buttonequals);
        buttondot =(Button)findViewById(R.id.buttondot);
        buttonclear=(Button) findViewById(R.id.buttonclear);
        button1 =(Button)findViewById(R.id.button1);
        button2 =(Button)findViewById(R.id.button2);
        button3 =(Button)findViewById(R.id.button3);
        button4 =(Button)findViewById(R.id.button4);
        button5 =(Button)findViewById(R.id.button5);
        button6 =(Button)findViewById(R.id.button6);
        button7 =(Button)findViewById(R.id.button7);
        button8 =(Button)findViewById(R.id.button8);
        button9 =(Button)findViewById(R.id.button9);
        button0 =(Button)findViewById(R.id.button0);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"1";
                a2=a2+"1";
                textinput.setText(a);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"2";
                a2=a2+"2";
                textinput.setText(a);
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"3";
                a2=a2+"3";
                textinput.setText(a);
            }
        });

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"4";
                a2=a2+"4";
                textinput.setText(a);
            }
        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"5";
                a2=a2+"5";
                textinput.setText(a);
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"6";
                a2=a2+"6";
                textinput.setText(a);
            }
        });

        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"7";
                a2=a2+"7";
                textinput.setText(a);
            }
        });

        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"8";
                a2=a2+"8";
                textinput.setText(a);
            }
        });

        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"9";
                a2=a2+"9";
                textinput.setText(a);
            }
        });

        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+"0";
                a2=a2+"0";
                textinput.setText(a);
            }
        });

        buttondot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=textinput.getText().toString();
                a=a+".";
                textinput.setText(a);
            }
        });

        buttonplus.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) {

                if (mathoperation == 0)
                {
                aa=a;
                mathoperation=1;
                a=a+" + ";
                textinput.setText(a);
                a2 = "0";
                }
            }
        });

        buttonminus.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) {

                if (mathoperation == 0)
                {
                    aa=a;
                    mathoperation=2;
                    a=a+" - ";
                    textinput.setText(a);
                    a2 = "0";
                }
            }
        });

        buttonmultiply.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) {

                if (mathoperation == 0)
                {
                    aa=a;
                    mathoperation=3;
                    a=a+" * ";
                    textinput.setText(a);
                    a2 = "0";
                }
            }
        });

        buttondivide.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                if (mathoperation == 0) {
                    aa = a;
                    mathoperation = 4;
                    a=a+" / ";
                    textinput.setText(a);
                    a2 = "0";
                }
            }
        });


        buttonclear.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) {
                aa = "";
                a = "0";
                mathoperation = 0;
                result = 0;
                textinput.setText("");
                textresult.setText("");
            }
        });

        buttonequals.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) {

                if(mathoperation==1){
                    value1=Double.parseDouble(aa);
                    value2=Double.parseDouble(a2);
                    result = value1 + value2;
                }
                else
                if(mathoperation==2){
                    value1=Double.parseDouble(aa);
                    value2=Double.parseDouble(a2);
                    result = value1 - value2;
                }else
                if(mathoperation==3){
                    value1=Double.parseDouble(aa);
                    value2=Double.parseDouble(a2);
                    result = value1 * value2;
                }
                else
                if(mathoperation==4){
                    value1=Double.parseDouble(aa);
                    value2=Double.parseDouble(a2);
                    result = value1 / value2;
                }
                n = Double.toString(result);
                textresult.setText(n);
                aa = "";
                a = "0";
                mathoperation = 0;
                result = 0;
            }
        });


















//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

编辑:如果在它们之后没有任何值与零不同,我想隐藏零。 假设我计算7 * 0.7并且仅显示6个值。 它会给我4.900000 我想要隐藏五个零。 怎么做?

1 个答案:

答案 0 :(得分:0)

以下是您的工作:

Set-Location

当您点击buttondot时,您还需要执行a2 = a2 +"。"

换句话说:

click Button1
    a=1
    a2=1
click Button.
    a=1.
    // add "a2=1."
click Button1
    a=1.1
    a2=11 <---
click ButtonMultiply
    operation=3
    aa=1.1
    a=1.1*
    a2=0
click Button1
    a=1.1*1
    a2=1
click Button.
    a=1.1*1.
    // add "a2=1."
click Button1
    a=1.1*1.1
    a2=11 <-----
click Buttonresult
    value1 = 1.1
    value2 = 11
    // value2 = 1.1
    result = 12.10000000000000000000001 
    // result = 1.210000000000000001

用于格式化之后:Round a double to 2 decimal places

PS:欢迎使用Stackoverflow !!