请教一个计算器的问题,如何实现负数计算?
百思不得其解,是别人的代码,在自己电脑运行,居然负数运算不了。 自己想不出它的代码了,要做负数运算的时候出错。 比如: -3-2 ;-10-5;-20-30 。 这种类型的计算。
package com.he;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class CalculatorActivity extends Activity {
//加plus 减subtract 乘multiply 除divide
private EditText etResult; //文本框显示结果
private Button btPlus; //加
private Button btSub; //减
private Button btMultiply; //乘
private Button btDivide; //除
private Button btDelete; //删
private Button btCe; //清空
private Button btPoint; //点
private Button btDeng; //等于
private Button bt0;
private Button bt1;
private Button bt2;
private Button bt3;
private Button bt4;
private Button bt5;
private Button bt6;
private Button bt7;
private Button bt8;
private Button bt9;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etResult=(EditText)findViewById(R.id.etResult);
btPlus=(Button)findViewById(R.id.btPlus);
btSub=(Button)findViewById(R.id.btSubtract);
btMultiply=(Button)findViewById(R.id.btMultiply);
btDivide=(Button)findViewById(R.id.btDivide);
btDelete=(Button)findViewById(R.id.btDelete);
btDeng=(Button)findViewById(R.id.btDeng);
btPoint=(Button)findViewById(R.id.btPoint);
btCe=(Button)findViewById(R.id.btCe);
bt0=(Button)findViewById(R.id.bt0);
bt1=(Button)findViewById(R.id.bt1);
bt2=(Button)findViewById(R.id.bt2);
bt3=(Button)findViewById(R.id.bt3);
bt4=(Button)findViewById(R.id.bt4);
bt5=(Button)findViewById(R.id.bt5);
bt6=(Button)findViewById(R.id.bt6);
bt7=(Button)findViewById(R.id.bt7);
bt8=(Button)findViewById(R.id.bt8);
bt9=(Button)findViewById(R.id.bt9);
etResult.setInputType(InputType.TYPE_NULL);
/*
* . 按钮
*/
btPoint.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
if(str1.equals("")){
etResult.setText("");
}else{
etResult.setText(str1+btPoint.getText().toString());
}
}
});
/*
* 0---------9的数字按钮
*/
bt0.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
if(str1.equals("")){
etResult.setText("");
}else{
etResult.setText(str1+bt0.getText().toString());
}
}
});
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
etResult.setText(str1+bt1.getText().toString());
}
});
bt2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
etResult.setText(str1+bt2.getText().toString());
}
});
bt3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
etResult.setText(str1+bt3.getText().toString());
}
});
bt4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
etResult.setText(str1+bt4.getText().toString());
}
});
bt5.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
etResult.setText(str1+bt5.getText().toString());
}
});
bt6.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
etResult.setText(str1+bt6.getText().toString());
}
});
bt7.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
etResult.setText(str1+bt7.getText().toString());
}
});
bt8.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
etResult.setText(str1+bt8.getText().toString());
}
});
bt9.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str1=etResult.getText().toString();
etResult.setText(str1+bt9.getText().toString());
}
});
/*
* ce键 清空
*/
btCe.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
etResult.setText("");
}
});
/*
* Delete键
*/
btDelete.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str=etResult.getText().toString();
if(str.length()==0){
etResult.setText("");
}else{
etResult.setText(str.subSequence(0, str.length()-1));
}
}
});
/*
* + 、-、 * 、/四个按钮
*/
btPlus.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
final String str=etResult.getText().toString();
if(str.equals("")){
etResult.setText("");
}else{
etResult.setText(str+btPlus.getText().toString());
}
}
});
btSub.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str=etResult.getText().toString();
if(str.equals("-")){
etResult.setText(str+btSub.getText().toString());
}else{
etResult.setText(str+btSub.getText().toString());
}
}
});
btMultiply.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str=etResult.getText().toString();
if(str.equals("")){
etResult.setText("");
}else{
etResult.setText(str+btMultiply.getText().toString());
}
}
});
btDivide.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str=etResult.getText().toString();
if(str.equals("")){
etResult.setText("");
}else{
etResult.setText(str+btDivide.getText().toString());
}
}
});
/*
* = 按钮
*/
btDeng.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String str=etResult.getText().toString();
/*if(str.charAt(str.length()-1)=='0' && str.charAt(str.length()-2)=='/'){
Toast.makeText(CalculatorActivity.this, "除数不能为0!", Toast.LENGTH_LONG).show();
}else{
}*/
if(str.indexOf('+')>0){
Double num1=Double.parseDouble(str.substring(0, str.indexOf('+')));
Double num2=Double.parseDouble(str.substring(str.indexOf('+')+1));
Double result=num1+num2;
etResult.setText(result.toString());
}
else if(str.indexOf('-')>0){
Double num1=Double.parseDouble(str.substring(0, str.indexOf('-')));
Double num2=Double.parseDouble(str.substring(str.indexOf('-')+1));
Double result=num1-num2;
etResult.setText(result.toString());
}
else if(str.indexOf('*')>0){
Double num1=Double.parseDouble(str.substring(0, str.indexOf('*')));
Double num2=Double.parseDouble(str.substring(str.indexOf('*')+1));
Double result=num1*num2;
etResult.setText(result.toString());
}else if(str.indexOf('/')>0){
Double num1=Double.parseDouble(str.substring(0, str.indexOf('/')));
Double num2=Double.parseDouble(str.substring(str.indexOf('/')+1));
if(num2==0){
Toast.makeText(CalculatorActivity.this, "除数不能为0!", Toast.LENGTH_LONG).show();
}else{
Double result=num1/num2;
etResult.setText(result.toString());
}
}
}
});
}
}
[解决办法]
曾经一时兴起,随便写写的:http://freish.iteye.com/admin/blogs/1526831
[解决办法]
不应该啊
num1-num2
你输入的num1和num2分别是什么呢?
[解决办法]
http://download.csdn.net/detail/peng_hao1988/1189218
给你个例子参考。