为了账号安全,请及时绑定邮箱和手机立即绑定

【大家来找BUG】Android萌新纯数学问题解决计算器的APK开发

标签:
Android

以下是MainActivity的代码,萌新作品,存在许多bug,欢迎大神们解决(⊙v⊙)

package com.example.funny;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TextView textView1;
    private Button bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0;
    private Button btAdd,btSubtract,btMultiply,btDivide;
    private Button btPoint,btSign;
    private Button btClear;

    private double num1=0,num2=0,result=0;
    private int numOperater1=1,numOperater2=1;
    private int operater=0;
    private double point=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator_layout);

        textView1 = (TextView) findViewById(R.id.textView1);

        bt1 = (Button) findViewById(R.id.button1);
        bt2 = (Button) findViewById(R.id.button2);
        bt3 = (Button) findViewById(R.id.button3);
        bt4 = (Button) findViewById(R.id.button4);
        bt5 = (Button) findViewById(R.id.button5);
        bt6 = (Button) findViewById(R.id.button6);
        bt7 = (Button) findViewById(R.id.button7);
        bt8 = (Button) findViewById(R.id.button8);
        bt9 = (Button) findViewById(R.id.button9);
        bt0 = (Button) findViewById(R.id.button0);

        btAdd = (Button) findViewById(R.id.buttonAdd);
        btSubtract = (Button) findViewById(R.id.buttonSubtract);
        btMultiply = (Button) findViewById(R.id.buttonMultiply);
        btDivide = (Button) findViewById(R.id.buttonDivide);

        btPoint = (Button) findViewById(R.id.buttonPoint);
        btSign = (Button) findViewById(R.id.buttonSign);

        btClear = (Button) findViewById(R.id.buttonClear);

        bt1.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+1;
                    else{
                        num1+=1/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+1;
                    else{
                        num2+=1/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt2.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+2;
                    else{
                        num1+=2/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+2;
                    else{
                        num2+=2/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt3.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+3;
                    else{
                        num1+=3/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+3;
                    else{
                        num2+=3/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt4.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+4;
                    else{
                        num1+=4/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+4;
                    else{
                        num2+=4/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt5.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+5;
                    else{
                        num1+=5/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+5;
                    else{
                        num2+=5/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt6.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+6;
                    else{
                        num1+=6/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+6;
                    else{
                        num2+=6/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt7.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+7;
                    else{
                        num1+=7/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+7;
                    else{
                        num2+=7/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt8.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+8;
                    else{
                        num1+=8/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+8;
                    else{
                        num2+=8/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt9.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10+9;
                    else{
                        num1+=9/point;
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10+9;
                    else{
                        num2+=9/point;
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });
        bt0.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(operater==0){
                    if(point == 1)
                        num1=num1*10;
                    else{
                        point = point*10;
                    }
                    textView1.setText(num1*numOperater1+"");
                }else{
                    if(point == 1)
                        num2=num2*10;
                    else{
                        point = point*10;
                    }
                    textView1.setText(num2*numOperater2+"");
                }
            }
        });

        btAdd.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(num2!=0)
                    getResult();
                operater = 1;
                textView1.setText(num2+"");
                point = 1;
            }
        });
        btSubtract.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(num2!=0)
                    getResult();
                if(operater==0 && num1==0){
                    numOperater1=-1;
                }else if(operater!=0 && num2==0){
                    numOperater2=-1;
                }else{
                    operater = 2;
                    textView1.setText(num2+"");
                    point = 1;
                }
            }
        });
        btMultiply.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(num2!=0)
                    getResult();
                operater = 3;
                textView1.setText(num2+"");
                point = 1;
            }
        });
        btDivide.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                if(num2!=0)
                    getResult();
                operater = 4;
                textView1.setText(num2+"");
                point = 1;
            }
        });

        btPoint.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                point = 10;
            }
        });
        btSign.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                getResult();
                textView1.setText(result+"");
            }
        });

        btClear.setOnClickListener(new MyOnClickLisenter(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                super.onClick(v);
                num1=0;num2=0;numOperater1=1;numOperater2=1;result=0;point=1;operater=0;
                textView1.setText(result+"");;
            }
        });
    }

    public void getResult(){
        switch (operater) {
        case 0:
            result = num1*numOperater1;
            break;
        case 1:
            result = num1*numOperater1+num2*numOperater2;
            break;
        case 2:
            result = num1*numOperater1-num2*numOperater2;
            break;
        case 3:
            result = num1*numOperater1*num2*numOperater2;
            break;
        case 4:
            result = (num1*numOperater1)/(num2*numOperater2);
            break;
        default:
            Toast.makeText(getApplicationContext(), "Error!!!", Toast.LENGTH_SHORT).show();
            break;
        }
        num1 = result;
        num2 = 0;
        numOperater2 = 1;
        operater = 0;
        point = 1;
    }
}

class MyOnClickLisenter implements OnClickListener{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消