计算器不能执行计算功能,运算结果始终为0

部分代码:

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("");

}

}


AbnerRoc
浏览 1428回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android