亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

各位大神,為什么我編寫的計算器不能計算出結果

各位大神,為什么我編寫的計算器不能計算出結果

AbnerRoc 2016-12-08 22:39:30
package?com.fang.mobileqq; import?com.fang.mobileqq.R; import?com.fang.mobileqq.R.id; import?android.app.Activity; import?android.content.Intent; import?android.os.Bundle; import?android.view.View; import?android.view.View.OnClickListener; import?android.widget.Button; import?android.widget.EditText; public?class?cal?extends?Activity?implements?OnClickListener{ ?EditText?t1; ?Button?bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0; ?Button?btplus,btminus,btmultiply,btdivide,btpoint,btclear,btdel,btequal; ?boolean?clear_flag;//清空標識 ?@Override protected?void?onCreate(Bundle?savedInstanceState)??{ ????????super.onCreate(savedInstanceState); ????????setContentView(R.layout.activity_main); ????????bt0=(Button)?findViewById(R.id.button16); ????????bt1=(Button)?findViewById(R.id.button13); ????????bt2=(Button)?findViewById(R.id.button14); ????????bt3=(Button)?findViewById(R.id.button15); ????????bt4=(Button)?findViewById(R.id.button9); ????????bt5=(Button)?findViewById(R.id.button10); ????????bt6=(Button)?findViewById(R.id.button11); ????????bt7=(Button)?findViewById(R.id.button5); ????????bt8=(Button)?findViewById(R.id.button6); ????????bt9=(Button)?findViewById(R.id.button7); ????????btplus=(Button)?findViewById(R.id.button12); ????????btminus=(Button)?findViewById(R.id.button8); ????????btmultiply=(Button)?findViewById(R.id.button4); ????????btdivide=(Button)?findViewById(R.id.button3); ????????btpoint=(Button)?findViewById(R.id.button17); ????????btclear=(Button)?findViewById(R.id.button1); ????????btdel=(Button)?findViewById(R.id.button2); ????????btequal=(Button)?findViewById(R.id.button18); ????????t1=(EditText)?findViewById(R.id.editText1); ???????? ????????bt0.setOnClickListener(this); ????????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); ????????btplus.setOnClickListener(this); ????????btminus.setOnClickListener(this); ????????btmultiply.setOnClickListener(this); ????????btdivide.setOnClickListener(this); ????????btpoint.setOnClickListener(this); ????????btclear.setOnClickListener(this); ????????btdel.setOnClickListener(this); ????????btequal.setOnClickListener(this); } @Override public?void?onClick(View?v)?{ //?TODO?Auto-generated?method?stub String?str=t1.getText().toString(); switch(v.getId()){ case?R.id.button16: case?R.id.button13: case?R.id.button14: case?R.id.button15: case?R.id.button9: case?R.id.button10: case?R.id.button11: case?R.id.button5: case?R.id.button6: case?R.id.button7: case?R.id.button17: if(clear_flag){ clear_flag=false; str=""; t1.setText(""); } ??t1.setText(str+((Button)v).getText()); ???break; case?R.id.button12: case?R.id.button8: case?R.id.button4: case?R.id.button3: if(clear_flag){ clear_flag=false; str=""; t1.setText(""); } t1.setText(str+"?"+((Button)v).getText()+"?"); break; case?R.id.button2: if(clear_flag){ clear_flag=false; str=""; t1.setText(""); } else?if(str!=null&&!str.equals("")){ t1.setText(str.substring(0,str.length()-1)); } ????break; case?R.id.button1: clear_flag=false; str=""; ????t1.setText("?"); break; case?R.id.button18: getResult(); break; ??} } private?void?getResult(){ String?exp=t1.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?s1=exp.substring(0,exp.indexOf("")); String?op=exp.substring(exp.indexOf("")+1,exp.indexOf("")+2); String?s2=exp.substring(exp.indexOf("")+3); if(!s1.equals("")&&!s2.equals("")){ double?d1=Double.parseDouble(s1); double?d2=Double.parseDouble(s2); 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(!s1.contains(".")&&!s2.contains(".")&&!op.equals('/')){ int?r=(int)result; t1.setText(r+""); }else{ t1.setText(result+"");} }else?if(!s1.equals("")&&s2.equals("")){ t1.setText(exp); }else?if(s1.equals("")&&!s2.equals("")){ double?d2=Double.parseDouble(s2); 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(!s2.contains(".")){ int?r=(int)result; t1.setText(r+"?"); }else{ t1.setText(result+"?"); } }else{ t1.setText(""); } ?} }
查看完整描述

2 回答

已采納
?
Listener呆

TA貢獻2條經驗 獲得超0個贊

首先,你這代碼看的讓人頭疼- -,大概看了一下,應該是你得到result后,沒有把他設置在edittext上面去

查看完整回答
反對 回復 2016-12-19
  • 2 回答
  • 0 關注
  • 1520 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號