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

安卓计算器小项目--减法结果为0,求指点,3Q!

安卓计算器小项目--减法结果为0,求指点,3Q!

different_sky 2016-10-13 18:28:46
public class MainActivity extends Activity implements OnClickListener{ private Button bt_c,bt_del,bt_chu,bt_cheng,bt_jia,bt_jian,bt_0,bt_1,bt_2,bt_3,               bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_dengyu,bt_dian; private EditText et_input; private boolean clear_flag;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.first);        bt_c=(Button) findViewById(R.id.bt_c);        bt_del=(Button) findViewById(R.id.bt_del);        bt_chu=(Button) findViewById(R.id.bt_chu);        bt_cheng=(Button) findViewById(R.id.bt_cheng);        bt_jia=(Button) findViewById(R.id.bt_jia);        bt_jian=(Button) findViewById(R.id.bt_jian);        bt_0=(Button) findViewById(R.id.bt_0);        bt_1=(Button) findViewById(R.id.bt_1);        bt_2=(Button) findViewById(R.id.bt_2);        bt_3=(Button) findViewById(R.id.bt_3);        bt_4=(Button) findViewById(R.id.bt_4);        bt_5=(Button) findViewById(R.id.bt_5);        bt_6=(Button) findViewById(R.id.bt_6);        bt_7=(Button) findViewById(R.id.bt_7);        bt_8=(Button) findViewById(R.id.bt_8);        bt_9=(Button) findViewById(R.id.bt_9);        bt_dengyu=(Button) findViewById(R.id.bt_dengyu);        bt_dian=(Button) findViewById(R.id.bt_dian);                et_input=(EditText) findViewById(R.id.input);                bt_0.setOnClickListener(this);        bt_1.setOnClickListener(this);        bt_2.setOnClickListener(this);        bt_3.setOnClickListener(this);        bt_4.setOnClickListener(this);        bt_5.setOnClickListener(this);        bt_6.setOnClickListener(this);        bt_7.setOnClickListener(this);        bt_8.setOnClickListener(this);        bt_9.setOnClickListener(this);        bt_dian.setOnClickListener(this);        bt_dengyu.setOnClickListener(this);        bt_c.setOnClickListener(this);        bt_del.setOnClickListener(this);        bt_jia.setOnClickListener(this);        bt_jian.setOnClickListener(this);        bt_cheng.setOnClickListener(this);        bt_chu.setOnClickListener(this);                et_input.setOnClickListener(this);    } public void onClick(View v) { // TODO Auto-generated method stub String str=et_input.getText().toString();//获取屏幕显示的数据 switch (v.getId()) { case R.id.bt_0: case R.id.bt_1: case R.id.bt_2: case R.id.bt_3: case R.id.bt_4: case R.id.bt_5: case R.id.bt_6: case R.id.bt_7: case R.id.bt_8: case R.id.bt_9: case R.id.bt_dian: if(clear_flag){ clear_flag=false; str=""; et_input.setText(""); } et_input.setText(str+((Button)v).getText()); //按那个键就会在屏幕上显示出之前str加上按得那个键值一起显示 break; case R.id.bt_jia: case R.id.bt_jian: case R.id.bt_cheng: case R.id.bt_chu: if(clear_flag){ clear_flag=false; str=""; et_input.setText(""); } et_input.setText(str+" "+((Button)v).getText()+" "); break; case R.id.bt_dengyu: getResult(); break; case R.id.bt_del: if(clear_flag){ clear_flag=false; str=""; et_input.setText(""); } else if(str!=null&&str!=""){ et_input.setText(str.substring(0,str.length()-1)); //如果str中不为空并且不是空字符串,就将str减一位显示出来 } break; case R.id.bt_c: clear_flag=false; str=""; et_input.setText("");//按C键就显示空字符串 break; default: break; } } private void getResult(){ String exp=et_input.getText().toString();//获取屏幕显示的数据 if(exp==null||exp.equals("")){ return; } if(!exp.contains(" ")){ return; } if(clear_flag){ clear_flag=false; return; } clear_flag=true; double r=0; int space =exp.indexOf(' ');//用于搜索空格位置    String s1 = exp.substring(0, space);//s1用于保存第一个运算数    String op = exp.substring(space + 1, space + 2);//op用于保存运算符    String s2 = exp.substring(space + 3);//s2用于保存第二个运算数        if(!s1.equals("")&&!s2.equals("")){        double arg1 = Double.parseDouble(s1);//将运算数从string转换为Single           double arg2 = Double.parseDouble(s2);                if(op.equals("+")){        r=arg1+arg2;        }else if (op.equals("-")) { r=arg1-arg2; }else if (op.equals("×")) { r=arg1*arg2; }else if (op.equals("÷")) { if(arg2==0){ r=0; } else{ r=arg1/arg2; } }        if(!s1.contains(".")&&!s2.contains(".")){        int result=(int) r;        et_input.setText(result+"");//如果都没小数点,就显示整数        }else {        et_input.setText(r+"");//否则,已小数点形式显示 } }else if(!s1.equals("")&&s2.equals("")){ et_input.setText(exp); }else if (s1.equals("")&&!s2.equals("")) {         double arg2 = Double.parseDouble(s2);                if(op.equals("+")){        r=0+arg2;        }else if (op.equals("-")) { r=0-arg2; }else if (op.equals("×")) { r=0; }else if (op.equals("÷")) { r=0;      }        if(!s2.contains(".")){        int result=(int) r;        et_input.setText(result+"");//如果都没小数点,就显示整数        }else {        et_input.setText(r+"");//否则,已小数点形式显示 }   }else { et_input.setText(""); } }   }
查看完整描述

2 回答

?
白丷露

TA贡献11条经验 获得超3个赞

哥们儿,你确定有问题?我用你代码测试了下,没问题呀。


查看完整回答
1 反对 回复 2016-10-16
  • 2 回答
  • 0 关注
  • 1207 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信