等号没效果
为什么我的代码要多一个Indexof方法?然后运算时都是等于0?然而这个Indexof方法要怎么写?
为什么我的代码要多一个Indexof方法?然后运算时都是等于0?然而这个Indexof方法要怎么写?
2016-05-23
package com.zhao.jisuanqi;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
// private static final String s2 = null;
private Button btn_0;
private Button btn_1;
private Button btn_2;
private Button btn_3;
private Button btn_4;
private Button btn_5;
private Button btn_6;
private Button btn_7;
private Button btn_8;
private Button btn_9;
private Button del;
private Button clear;
private Button cheng;
private Button jia;
private Button chu;
private Button jian;
private Button dian;
private Button denghao;
private EditText et_input;
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);
dian=(Button) findViewById(R.id.dian);
del=(Button) findViewById(R.id.del);
clear=(Button) findViewById(R.id.clear);
jia=(Button) findViewById(R.id.jia);
jian=(Button) findViewById(R.id.jian);
cheng=(Button) findViewById(R.id.cheng);
chu=(Button) findViewById(R.id.chu);
et_input=(EditText) findViewById(R.id.et_input);
denghao=(Button) findViewById(R.id.denghao);
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);
clear.setOnClickListener(this);
del.setOnClickListener(this);
jia.setOnClickListener(this);
jian.setOnClickListener(this);
cheng.setOnClickListener(this);
chu.setOnClickListener(this);
dian.setOnClickListener(this);
denghao.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str=et_input.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.dian:
if (clear_flag) {
clear_flag=false;
str="";
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.jia:
case R.id.jian:
case R.id.cheng:
case R.id.chu:
if (clear_flag) {
clear_flag=false;
str="";
et_input.setText("");
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.del:
if (clear_flag) {
clear_flag=false;
str="";
et_input.setText("");
}
else if (str!=null&&!str.equals("")) {
et_input.setText(str.substring(0,str.length()-1));
}
break;
case R.id.clear:
clear_flag=false;
str="";
et_input.setText("");
break;
case R.id.denghao:
getResult();
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 result=0;
String s1=exp.substring(0, indexOf(" "));//运算符前面的字符串
String op=exp.substring(indexOf(" ")+1, indexOf(" ")+2);//运算符
String s2=exp.substring(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) { //分母等于0
result=0;
}else{
result=d1/d2;
}
}
if (!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
int r=(int)result;
et_input.setText(r+"");
}else{
et_input.setText(result+"");
}
}else if(!s1.equals("")&&s2.equals("")){
et_input.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;
et_input.setText(r+"");
}else{
et_input.setText(result+"");
}
} else{
et_input.setText("");
}
}
private int indexOf(String string) {
return 0;
}}
举报