• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

点击“等号”程序就退出运行了,哪位大神帮我看下哪里有问题?

package com.huihui.administrator.mycalculator;import android.app.Activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity implements View.OnClickListener {   Button btn_0;   Button btn_1;   Button btn_2;   Button btn_3;   Button btn_4;   Button btn_5;   Button btn_6;   Button btn_7;   Button btn_8;   Button btn_9;   Button btn_plus;   Button btn_min;   Button btn_mul;   Button btn_div;   Button btn_dot;   Button btn_eql;   Button btn_C;   Button btn_Del;   TextView txt;   boolean clear_flag;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn_0= (Button) findViewById(R.id.btn_0);        btn_1= (Button) findViewById(R.id.btn_1);        btn_2= (Button) findViewById(R.id.btn_2);        btn_3= (Button) findViewById(R.id.btn_3);        btn_4= (Button) findViewById(R.id.btn_4);        btn_5= (Button) findViewById(R.id.btn_5);        btn_6= (Button) findViewById(R.id.btn_6);        btn_7= (Button) findViewById(R.id.btn_7);        btn_8= (Button) findViewById(R.id.btn_8);        btn_9= (Button) findViewById(R.id.btn_9);        btn_plus= (Button) findViewById(R.id.btn_PLU);        btn_min= (Button) findViewById(R.id.btn_MIN);        btn_mul= (Button) findViewById(R.id.btn_MUL);        btn_div= (Button) findViewById(R.id.btn_div);        btn_eql= (Button) findViewById(R.id.btn_Eql);        btn_dot= (Button) findViewById(R.id.btn_DOT);        btn_C= (Button) findViewById(R.id.btn_C);        btn_Del= (Button) findViewById(R.id.btn_DEL);        txt= (TextView) findViewById(R.id.textView);        btn_0.setOnClickListener(this);        btn_1.setOnClickListener(this);        btn_2.setOnClickListener(this);        btn_3.setOnClickListener(this);        btn_4.setOnClickListener(this);        btn_5.setOnClickListener(this);        btn_6.setOnClickListener(this);        btn_7.setOnClickListener(this);        btn_8.setOnClickListener(this);        btn_9.setOnClickListener(this);        btn_plus.setOnClickListener(this);        btn_min.setOnClickListener(this);        btn_mul.setOnClickListener(this);        btn_div.setOnClickListener(this);        btn_C.setOnClickListener(this);        btn_Del.setOnClickListener(this);        btn_dot.setOnClickListener(this);        btn_eql.setOnClickListener(this);    }    @Override    public void onClick(View v) {         String str = txt.getText().toString();        switch (v.getId()){            case R.id.btn_0:            case R.id.btn_1:            case R.id.btn_2:            case R.id.btn_3:            case R.id.btn_4:            case R.id.btn_5:            case R.id.btn_6:            case R.id.btn_7:            case R.id.btn_8:            case R.id.btn_9:            case R.id.btn_DOT:                if (clear_flag){                    clear_flag=false;                    str="";                    txt.setText("");                }                 txt.setText(str+((Button)v).getText());            break;            case R.id.btn_div:            case R.id.btn_MIN:            case R.id.btn_MUL:            case R.id.btn_PLU:                if (clear_flag){                    clear_flag=false;                    str="";                    txt.setText("");                }                txt.setText(str+" "+((Button)v).getText()+" ");            break;            case R.id.btn_C:                clear_flag=false;                str="";                txt.setText("");                break;            case R.id.btn_DEL:                if (clear_flag){                    clear_flag=false;                    str="";                    txt.setText("");                }                else  if (str!=null&&str!=""){                    txt.setText(str.substring(0,str.length()-1));                }                break;            case R.id.btn_Eql:                getResult();                break;        }    }    public void getResult(){        String exp = txt.getText().toString();        if(exp==null||exp.equals("")){              return;        }        if (!exp.contains(" ")){             return;        }        if (clear_flag){            clear_flag=false;            return;        }        clear_flag=true;        double result =0;        String A=exp.substring(0,exp.indexOf(" "));        String OP=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);        String B=exp.substring(exp.indexOf(" ")+3);        if (!A.equals("")&&!B.equals("")){            double d1=Double.parseDouble(A);            double d2=Double.parseDouble(B);            if (OP.equals("+")){                result=d1+d2;            }            else if (OP.equals("-")){                result=d1-d2;            }            else if (OP.equals("×")){                result=d1*d2;            }            else if (OP.equals("÷")){                if (d2==0) {                    result = 0;                }                else {                    result = d1/d2;                }            }            if (!A.contains(".")&&!B.contains(".")&&!OP.equals("÷")){                int r = (int)result;                txt.setText(r+"");            }            else {                txt.setText(result+"");            }        }         else if(!A.equals("")&&B.equals("")){                txt.setText(exp);        }        else if(A.equals("")&&!B.equals("")){            double d2=Double.parseDouble(B);            if (OP.equals("+")){                result=0+d2;            }            else if (OP.equals("-")){                result=0-d2;            }            else if (OP.equals("×")){                result=0;            }            else if (OP.equals("÷")){                result = 0;            }            if (!B.contains(".")){                int r = (int)result;                txt.setText(r+"");            }            else {                txt.setText(result+"");            }        }else {                txt.setText("");        }    }}

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与2

txt的初始值是" "吧

使用道具 举报

只要一开始运行时点一下Button“C”,就可以。还没找到是什么原因

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!