一般是什么问题导致不能小数运算
字符串的类型我转换为double了
字符串的类型我转换为double了
2016-09-11
public class MainActivity extends ActionBarActivity implements OnClickListener { private Button btc; private Button btd; private Button btv; private Button btm; private Button bt9; private Button bt8; private Button bt7; private Button bt6; private Button bt5; private Button bt4; private Button bt3; private Button bt2; private Button bt1; private Button bt0; private Button btp; private Button btj; private Button bti; private Button bta; private EditText edt; boolean flag ; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btc = (Button) findViewById(R.id.clear); btd =(Button) findViewById(R.id.Delate); btv =(Button) findViewById(R.id.chu); btm = (Button) findViewById(R.id.mut); bt9 = (Button) findViewById(R.id.btn_9); bt8 = (Button) findViewById(R.id.btn_8); bt7 =(Button) findViewById(R.id.btn_7); bt6 = (Button) findViewById(R.id.btn_6); bt5 = (Button) findViewById(R.id.btn_5); bt4 =(Button) findViewById(R.id.btn_4); bt3 = (Button) findViewById(R.id.btn_3); bt2 =(Button) findViewById(R.id.btn_2); bt1 =(Button) findViewById(R.id.btn_1); bt0 =(Button) findViewById(R.id.btn_0); btp =(Button) findViewById(R.id.btn_point); btj =(Button) findViewById(R.id.jian); bti =(Button) findViewById(R.id.output); bta =(Button) findViewById(R.id.add); edt =(EditText) findViewById(R.id.input); btc.setOnClickListener(this); btd.setOnClickListener(this); btv.setOnClickListener(this); btm.setOnClickListener(this); bt9.setOnClickListener(this); bt8.setOnClickListener(this); bt7.setOnClickListener(this); bt6.setOnClickListener(this); bt5.setOnClickListener(this); bt4.setOnClickListener(this); bt3.setOnClickListener(this); bt2.setOnClickListener(this); bt1.setOnClickListener(this); bt0.setOnClickListener(this); btp.setOnClickListener(this); btj.setOnClickListener(this); bti.setOnClickListener(this); bta.setOnClickListener(this); edt.setOnClickListener(this); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } @Override public void onClick(View v) { // TODO Auto-generated method stub String str = edt.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_point: if(flag){ flag=false; edt.setText(""); } edt.setText(str+((Button)v).getText()); break; case R.id.chu: case R.id.jian: case R.id.mut: case R.id.add: if(flag){ flag=false; edt.setText(""); } edt.setText(str+" "+((Button)v).getText()+" "); break; case R.id.clear: flag=false; edt.setText(""); break; case R.id.Delate: if (str!=null&&str.equals("")) { edt.setText(str.substring(0, str.length()-1)); } break; case R.id.output: getrt(); break; default: break; } } private void getrt(){ String exp = edt.getText().toString(); if(exp ==null||exp.equals("")){ return; } if(!exp.contains("")){ return; } flag = true; double r3 =0; String r1=exp.substring(0, exp.indexOf(" ")); String op=exp.substring(exp.indexOf(" ")+1, exp.indexOf(" ")+2); String r2=exp.substring(exp.indexOf(" ")+2); if(!r1.equals("")&&!r2.equals("")){ double e1 = Double.parseDouble(r1); double e2 = Double.parseDouble(r2); if(op.equals("+")){ r3 = e1 + e2; }else if(op.equals("-")){ r3 =e1-e2; }else if(op.equals("/")){ r3 = e1/e2; } else if(op.equals("X")){ if(e2==0){ r3 =0; }else{ r3 = e1*e2; } } if(!r1.contains(".")&&!r2.contains(".")){ int r =(int) r3; edt.setText(r+""); } } } }
举报