android简单的计算器有错,求好心人帮忙(80分)
Java codepackage com.example.calcultor;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity implements OnClickListener{ static String number1=null; static String number2=null; static String number3=null;//实现连等功能 static double result=0; static Boolean checkNum=false; static Boolean check_ls=false;//实现连等功能 static int op=0; private Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9; private Button btn_multiply,btn_divide,btn_dot,btn_clear,btn_plus,btn_reduce,btn_result; private EditText et; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn0=(Button)findViewById(R.id.zero); btn1=(Button)findViewById(R.id.one); btn2=(Button)findViewById(R.id.two); btn3=(Button)findViewById(R.id.three); btn4=(Button)findViewById(R.id.four); btn5=(Button)findViewById(R.id.five); btn6=(Button)findViewById(R.id.six); btn7=(Button)findViewById(R.id.seven); btn8=(Button)findViewById(R.id.eight); btn9=(Button)findViewById(R.id.nine); btn_reduce=(Button)findViewById(R.id.reduce); btn_multiply=(Button)findViewById(R.id.multiply); btn_divide=(Button)findViewById(R.id.divide); btn_dot=(Button)findViewById(R.id.dot); btn_clear=(Button)findViewById(R.id.clear); btn_plus=(Button)findViewById(R.id.plus); btn_result=(Button)findViewById(R.id.result); et=(EditText)findViewById(R.id.et); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); btn7.setOnClickListener(this); btn8.setOnClickListener(this); btn9.setOnClickListener(this); btn0.setOnClickListener(this); btn_reduce.setOnClickListener(this); btn_multiply.setOnClickListener(this); btn_divide.setOnClickListener(this); btn_dot.setOnClickListener(this); btn_clear.setOnClickListener(this); btn_plus.setOnClickListener(this); btn_result.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(View v) { switch(v.getId()){ default: break; case R.id.one:setTextValue(String.valueOf(R.id.one)); break; case R.id.two:setTextValue(String.valueOf(R.id.two)); break; case R.id.three:setTextValue(String.valueOf(R.id.three)); break; case R.id.four:setTextValue(String.valueOf(R.id.four)); break; case R.id.five:setTextValue(String.valueOf(R.id.five)); break; case R.id.six:setTextValue(String.valueOf(R.id.six)); break; case R.id.seven:setTextValue(String.valueOf(R.id.seven)); break; case R.id.eight:setTextValue(String.valueOf(R.id.eight)); break; case R.id.nine:setTextValue(String.valueOf(R.id.nine)); break; case R.id.zero: if(et.getText().toString()!=null) setTextValue(String.valueOf(R.id.zero)); else break; case R.id.plus:{op=1;get_number(); break;} case R.id.reduce:{op=2;get_number(); break;} case R.id.multiply:{op=3;get_number(); break;} case R.id.divide:{op=4;get_number(); break;} case R.id.dot: if(et.getText().toString()!=null) setTextValue(String.valueOf(R.id.dot)); else break; case R.id.clear:All_clear(); break; case R.id.result:print_result(); break; } } private void setTextValue(String str){ et.append(str); } private void get_number(){ checkNum=!checkNum; if(checkNum){ number1=et.getText().toString(); et.setText(""); } else number2=et.getText().toString(); } private void All_clear(){ number1=""; number2=""; number3="";//实现连等功能 result=0; checkNum=false; check_ls=false;//实现连等功能 op=0; } private void print_result(){ if(!check_ls){ switch(op){ case 1:result=Double.valueOf(number1)+Double.valueOf(number2);break; case 2:result=Double.valueOf(number1)-Double.valueOf(number2);break; case 3:result=Double.valueOf(number1)*Double.valueOf(number2);break; case 4:result=Double.valueOf(number1)/Double.valueOf(number2);break; } check_ls=true; } else { switch(op){ case 1:result+=Double.valueOf(number3);break; case 2:result-=Double.valueOf(number3);break; case 3:result*=Double.valueOf(number3);break; case 4:result/=Double.valueOf(number3);break; } et.setText(String.valueOf(result)); } } }