问题描述
- android下的计算器中的退格功能实现不了,麻烦各位大神啦,谢谢!!
-
private EditText output = null;private EditText input = null;
private Button btn0 = null; private Button btn1 = null; private Button btn2 = null; private Button btn3 = null; private Button btn4 = null; private Button btn5 = null; private Button btn6 = null; private Button btn7 = null; private Button btn8 = null; private Button btn9 = null; private Button btnadd = null; private Button btnsubtract = null; private Button btnmultiply = null; private Button btndivide = null; private Button btnclear = null; private Button btnresult = null; private Button btndot = null; private Button btnbackspace = null; private Button btnkaifang = null; private Button btnpingfang = null; private String str = "";// 保存数字 private String strold = "";// 原数字 private char act = ' ';// 记录“加减乘除等于”符号 private int count = 0;// 判断要计算的次数,如果超过一个符号,先算出来一部分 private Double result = 0.0;// 计算的输出结果 private Boolean errBoolean = false;// 有错误的时候为true,无错为false private Boolean flagBoolean = false;// 一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息 private Boolean flagDot = false; // 小数点标志位 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); output = (EditText) rootView.findViewById(R.id.output); input = (EditText) rootView.findViewById(R.id.input); btn0 = (Button) rootView.findViewById(R.id.zero); btn1 = (Button) rootView.findViewById(R.id.one); btn2 = (Button) rootView.findViewById(R.id.two); btn3 = (Button) rootView.findViewById(R.id.three); btn4 = (Button) rootView.findViewById(R.id.four); btn5 = (Button) rootView.findViewById(R.id.five); btn6 = (Button) rootView.findViewById(R.id.six); btn7 = (Button) rootView.findViewById(R.id.seven); btn8 = (Button) rootView.findViewById(R.id.eight); btn9 = (Button) rootView.findViewById(R.id.nine); btnadd = (Button) rootView.findViewById(R.id.add); btnsubtract = (Button) rootView.findViewById(R.id.subtract); btnmultiply = (Button) rootView.findViewById(R.id.multiply); btndivide = (Button) rootView.findViewById(R.id.divide); btnclear = (Button) rootView.findViewById(R.id.clear); btnresult = (Button) rootView.findViewById(R.id.result); btndot = (Button) rootView.findViewById(R.id.dot); btnbackspace = (Button) rootView.findViewById(R.id.zhengfu); btnkaifang = (Button) rootView.findViewById(R.id.kaifang); btnpingfang = (Button) rootView.findViewById(R.id.pingfang); // 设置按钮侦听事件 btn0.setOnClickListener(new BtnOnClickListener()); btn1.setOnClickListener(new BtnOnClickListener()); btn2.setOnClickListener(new BtnOnClickListener()); btn3.setOnClickListener(new BtnOnClickListener()); btn4.setOnClickListener(new BtnOnClickListener()); btn5.setOnClickListener(new BtnOnClickListener()); btn6.setOnClickListener(new BtnOnClickListener()); btn7.setOnClickListener(new BtnOnClickListener()); btn8.setOnClickListener(new BtnOnClickListener()); btn9.setOnClickListener(new BtnOnClickListener()); // 执行运算 btnadd.setOnClickListener(new BtnOnClickListener()); btnsubtract.setOnClickListener(new BtnOnClickListener()); btnmultiply.setOnClickListener(new BtnOnClickListener()); btndivide.setOnClickListener(new BtnOnClickListener()); btnclear.setOnClickListener(new BtnOnClickListener()); btnresult.setOnClickListener(new BtnOnClickListener()); btndot.setOnClickListener(new BtnOnClickListener()); btnbackspace.setOnClickListener(new BtnOnClickListener()); btnkaifang.setOnClickListener(new BtnOnClickListener()); btnpingfang.setOnClickListener(new BtnOnClickListener()); return rootView; } class BtnOnClickListener implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { // 输入数字 case R.id.zero: num(0); break; case R.id.one: num(1); break; case R.id.two: num(2); break; case R.id.three: num(3); break; case R.id.four: num(4); break; case R.id.five: num(5); break; case R.id.six: num(6); break; case R.id.seven: num(7); break; case R.id.eight: num(8); break; case R.id.nine: num(9); break; case R.id.dot: dot(); break; // 执行运算 // 退格功能 case R.id.zhengfu: backspace(); break; case R.id.kaifang: kaifang(); break; case R.id.pingfang: pingfang(); break; case R.id.add: add(); break; case R.id.subtract: sub(); break; case R.id.multiply: multiply(); break; case R.id.divide: divide(); break; case R.id.clear: clear(); break; // 计算结果 case R.id.result: result(); if (!errBoolean && flagBoolean) { output.setText(String.valueOf(result)); } default: break; } // if(strold==""&&act==' '&&str=="") // { // input.setText(""); // } // else { // input.setText(strold+act+str); // } if (act == 'k') { input.setText(strold); } else { input.setText(strold + act + str); } if (act == 'p') { input.setText(strold); } else { input.setText(strold + act + str); } output.setText(String.valueOf(result)); } private void kaifang() { // TODO Auto-generated method stub if (flagBoolean) { act = 'k'; strold = str; // str=""; result(); } } private void pingfang() { // TODO Auto-generated method stub if (flagBoolean) { act = 'p'; strold = str; // str=""; result(); } } //退格功能 private void backspace() { // TODO Auto-generated method stub String str1=input.getText().toString(); if(input.length()>0){ input.setText(str1.substring(0, str1.length()-1)); } } private void dot() { // TODO Auto-generated method stub if (!flagDot) { str = str + "."; flagBoolean = false; flagDot = true; } // str=str+i; // flagBoolean=false; } //清空 private void clear() { // TODO Auto-generated method stub str = strold = ""; count = 0; act = ' '; result = 0.0; flagBoolean = false; flagDot = false; input.setText(strold + act + str); output.setText(""); } private void divide() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '/'; flagBoolean = false; } } private void multiply() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '*'; flagBoolean = false; } } private void sub() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '-'; flagBoolean = false; } } private void add() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '+'; flagBoolean = false; } } private void check() { // TODO Auto-generated method stub if (count >= 1) { result(); str = String.valueOf(result); } strold = str; str = ""; count++; flagDot = false; } // 计算输出结果 private void result() { // TODO Auto-generated method stub if (flagBoolean) { Double a, b; // if(act=='k'||act=='c') // { // a=Double.parseDouble(strold); // } // else { // a=Double.parseDouble(strold); // b=Double.parseDouble(str); // } a = Double.parseDouble(strold); b = Double.parseDouble(str); if (b == 0 && act == '/') { clear(); output.setText("除数不能为零!"); errBoolean = true; } if (!errBoolean) { switch (act) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; case 'k': result = Math.sqrt(a); break; case 'p': result = Math.pow(a,2); break; default: break; } } } } private void num(int i) { // TODO Auto-generated method stub str = str + String.valueOf(i); flagBoolean = true; } }
解决方案
Android实现计算器功能
Android计算器功能的实现
Android实现简单计算器功能(Button控件实现)
解决方案二:
无法直视,这代码黏贴的,怎么看怎么晕
解决方案三:
```package com.example.t;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().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() {
}
private EditText output = null;
private EditText input = null;
private Button btn0 = null;
private Button btn1 = null;
private Button btn2 = null;
private Button btn3 = null;
private Button btn4 = null;
private Button btn5 = null;
private Button btn6 = null;
private Button btn7 = null;
private Button btn8 = null;
private Button btn9 = null;
private Button btnadd = null;
private Button btnsubtract = null;
private Button btnmultiply = null;
private Button btndivide = null;
private Button btnclear = null;
private Button btnresult = null;
private Button btndot = null;
private Button btnbackspace = null;
private Button btnkaifang = null;
private Button btnpingfang = null;
private String str = "";// 保存数字
private String strold = "";// 原数字
private char act = ' ';// 记录“加减乘除等于”符号
private int count = 0;// 判断要计算的次数,如果超过一个符号,先算出来一部分
private Double result = 0.0;// 计算的输出结果
private Boolean errBoolean = false;// 有错误的时候为true,无错为false
private Boolean flagBoolean = false;// 一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息
private Boolean flagDot = false; // 小数点标志位
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
output = (EditText) rootView.findViewById(R.id.output);
input = (EditText) rootView.findViewById(R.id.input);
btn0 = (Button) rootView.findViewById(R.id.zero);
btn1 = (Button) rootView.findViewById(R.id.one);
btn2 = (Button) rootView.findViewById(R.id.two);
btn3 = (Button) rootView.findViewById(R.id.three);
btn4 = (Button) rootView.findViewById(R.id.four);
btn5 = (Button) rootView.findViewById(R.id.five);
btn6 = (Button) rootView.findViewById(R.id.six);
btn7 = (Button) rootView.findViewById(R.id.seven);
btn8 = (Button) rootView.findViewById(R.id.eight);
btn9 = (Button) rootView.findViewById(R.id.nine);
btnadd = (Button) rootView.findViewById(R.id.add);
btnsubtract = (Button) rootView.findViewById(R.id.subtract);
btnmultiply = (Button) rootView.findViewById(R.id.multiply);
btndivide = (Button) rootView.findViewById(R.id.divide);
btnclear = (Button) rootView.findViewById(R.id.clear);
btnresult = (Button) rootView.findViewById(R.id.result);
btndot = (Button) rootView.findViewById(R.id.dot);
btnbackspace = (Button) rootView.findViewById(R.id.zhengfu);
btnkaifang = (Button) rootView.findViewById(R.id.kaifang);
btnpingfang = (Button) rootView.findViewById(R.id.pingfang);
// 设置按钮侦听事件
btn0.setOnClickListener(new BtnOnClickListener());
btn1.setOnClickListener(new BtnOnClickListener());
btn2.setOnClickListener(new BtnOnClickListener());
btn3.setOnClickListener(new BtnOnClickListener());
btn4.setOnClickListener(new BtnOnClickListener());
btn5.setOnClickListener(new BtnOnClickListener());
btn6.setOnClickListener(new BtnOnClickListener());
btn7.setOnClickListener(new BtnOnClickListener());
btn8.setOnClickListener(new BtnOnClickListener());
btn9.setOnClickListener(new BtnOnClickListener());
// 执行运算
btnadd.setOnClickListener(new BtnOnClickListener());
btnsubtract.setOnClickListener(new BtnOnClickListener());
btnmultiply.setOnClickListener(new BtnOnClickListener());
btndivide.setOnClickListener(new BtnOnClickListener());
btnclear.setOnClickListener(new BtnOnClickListener());
btnresult.setOnClickListener(new BtnOnClickListener());
btndot.setOnClickListener(new BtnOnClickListener());
btnbackspace.setOnClickListener(new BtnOnClickListener());
btnkaifang.setOnClickListener(new BtnOnClickListener());
btnpingfang.setOnClickListener(new BtnOnClickListener());
return rootView;
}
class BtnOnClickListener implements OnClickListener {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
// 输入数字
case R.id.zero:
num(0);
break;
case R.id.one:
num(1);
break;
case R.id.two:
num(2);
break;
case R.id.three:
num(3);
break;
case R.id.four:
num(4);
break;
case R.id.five:
num(5);
break;
case R.id.six:
num(6);
break;
case R.id.seven:
num(7);
break;
case R.id.eight:
num(8);
break;
case R.id.nine:
num(9);
break;
case R.id.dot:
dot();
break;
// 执行运算
// 退格功能
case R.id.zhengfu:
backspace();
break;
case R.id.kaifang:
kaifang();
break;
case R.id.pingfang:
pingfang();
break;
case R.id.add:
add();
break;
case R.id.subtract:
sub();
break;
case R.id.multiply:
multiply();
break;
case R.id.divide:
divide();
break;
case R.id.clear:
clear();
break;
// 计算结果
case R.id.result:
result();
if (!errBoolean && flagBoolean) {
output.setText(String.valueOf(result));
}
default:
break;
}
// if(strold==""&&act==' '&&str=="")
// {
// input.setText("");
// }
// else {
// input.setText(strold+act+str);
// }
if (act == 'k') {
input.setText(strold);
} else {
input.setText(strold + act + str);
}
if (act == 'p') {
input.setText(strold);
} else {
input.setText(strold + act + str);
}
output.setText(String.valueOf(result));
}
private void kaifang() {
// TODO Auto-generated method stub
if (flagBoolean) {
act = 'k';
strold = str;
// str="";
result();
}
}
private void pingfang() {
// TODO Auto-generated method stub
if (flagBoolean) {
act = 'p';
strold = str;
// str="";
result();
}
}
//退格功能
private void backspace() {
// TODO Auto-generated method stub
String str1=input.getText().toString();
if(input.length()>0){
input.setText(str1.substring(0, str1.length()-1));
}
}
private void dot() {
// TODO Auto-generated method stub
if (!flagDot) {
str = str + ".";
flagBoolean = false;
flagDot = true;
}
// str=str+i;
// flagBoolean=false;
}
//清空
private void clear() {
// TODO Auto-generated method stub
str = strold = "";
count = 0;
act = ' ';
result = 0.0;
flagBoolean = false;
flagDot = false;
input.setText(strold + act + str);
output.setText("");
}
private void divide() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '/';
flagBoolean = false;
}
}
private void multiply() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '*';
flagBoolean = false;
}
}
private void sub() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '-';
flagBoolean = false;
}
}
private void add() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '+';
flagBoolean = false;
}
}
private void check() {
// TODO Auto-generated method stub
if (count >= 1) {
result();
str = String.valueOf(result);
}
strold = str;
str = "";
count++;
flagDot = false;
}
// 计算输出结果
private void result() {
// TODO Auto-generated method stub
if (flagBoolean) {
Double a, b;
// if(act=='k'||act=='c')
// {
// a=Double.parseDouble(strold);
// }
// else {
// a=Double.parseDouble(strold);
// b=Double.parseDouble(str);
// }
a = Double.parseDouble(strold);
b = Double.parseDouble(str);
if (b == 0 && act == '/') {
clear();
output.setText("除数不能为零!");
errBoolean = true;
}
if (!errBoolean) {
switch (act) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
case 'k':
result = Math.sqrt(a);
break;
case 'p':
result = Math.pow(a,2);
break;
default:
break;
}
}
}
}
private void num(int i) {
// TODO Auto-generated method stub
str = str + String.valueOf(i);
flagBoolean = true;
}
}
}
}
解决方案四:
android下的计算器中的退格功能实现不了 (为了方便大家找出错误,我重新整理了下代码) ,提问者和我是同一人,谢谢
package com.example.t;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().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() {
}
private EditText output = null;
private EditText input = null;
private Button btn0 = null;
private Button btn1 = null;
private Button btn2 = null;
private Button btn3 = null;
private Button btn4 = null;
private Button btn5 = null;
private Button btn6 = null;
private Button btn7 = null;
private Button btn8 = null;
private Button btn9 = null;
private Button btnadd = null;
private Button btnsubtract = null;
private Button btnmultiply = null;
private Button btndivide = null;
private Button btnclear = null;
private Button btnresult = null;
private Button btndot = null;
private Button btnbackspace = null;
private Button btnkaifang = null;
private Button btnpingfang = null;
private String str = "";// 保存数字
private String strold = "";// 原数字
private char act = ' ';// 记录“加减乘除等于”符号
private int count = 0;// 判断要计算的次数,如果超过一个符号,先算出来一部分
private Double result = 0.0;// 计算的输出结果
private Boolean errBoolean = false;// 有错误的时候为true,无错为false
private Boolean flagBoolean = false;// 一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息
private Boolean flagDot = false; // 小数点标志位
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
output = (EditText) rootView.findViewById(R.id.output);
input = (EditText) rootView.findViewById(R.id.input);
btn0 = (Button) rootView.findViewById(R.id.zero);
btn1 = (Button) rootView.findViewById(R.id.one);
btn2 = (Button) rootView.findViewById(R.id.two);
btn3 = (Button) rootView.findViewById(R.id.three);
btn4 = (Button) rootView.findViewById(R.id.four);
btn5 = (Button) rootView.findViewById(R.id.five);
btn6 = (Button) rootView.findViewById(R.id.six);
btn7 = (Button) rootView.findViewById(R.id.seven);
btn8 = (Button) rootView.findViewById(R.id.eight);
btn9 = (Button) rootView.findViewById(R.id.nine);
btnadd = (Button) rootView.findViewById(R.id.add);
btnsubtract = (Button) rootView.findViewById(R.id.subtract);
btnmultiply = (Button) rootView.findViewById(R.id.multiply);
btndivide = (Button) rootView.findViewById(R.id.divide);
btnclear = (Button) rootView.findViewById(R.id.clear);
btnresult = (Button) rootView.findViewById(R.id.result);
btndot = (Button) rootView.findViewById(R.id.dot);
btnbackspace = (Button) rootView.findViewById(R.id.zhengfu);
btnkaifang = (Button) rootView.findViewById(R.id.kaifang);
btnpingfang = (Button) rootView.findViewById(R.id.pingfang);
// 设置按钮侦听事件
btn0.setOnClickListener(new BtnOnClickListener());
btn1.setOnClickListener(new BtnOnClickListener());
btn2.setOnClickListener(new BtnOnClickListener());
btn3.setOnClickListener(new BtnOnClickListener());
btn4.setOnClickListener(new BtnOnClickListener());
btn5.setOnClickListener(new BtnOnClickListener());
btn6.setOnClickListener(new BtnOnClickListener());
btn7.setOnClickListener(new BtnOnClickListener());
btn8.setOnClickListener(new BtnOnClickListener());
btn9.setOnClickListener(new BtnOnClickListener());
// 执行运算
btnadd.setOnClickListener(new BtnOnClickListener());
btnsubtract.setOnClickListener(new BtnOnClickListener());
btnmultiply.setOnClickListener(new BtnOnClickListener());
btndivide.setOnClickListener(new BtnOnClickListener());
btnclear.setOnClickListener(new BtnOnClickListener());
btnresult.setOnClickListener(new BtnOnClickListener());
btndot.setOnClickListener(new BtnOnClickListener());
btnbackspace.setOnClickListener(new BtnOnClickListener());
btnkaifang.setOnClickListener(new BtnOnClickListener());
btnpingfang.setOnClickListener(new BtnOnClickListener());
return rootView;
}
class BtnOnClickListener implements OnClickListener {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
// 输入数字
case R.id.zero:
num(0);
break;
case R.id.one:
num(1);
break;
case R.id.two:
num(2);
break;
case R.id.three:
num(3);
break;
case R.id.four:
num(4);
break;
case R.id.five:
num(5);
break;
case R.id.six:
num(6);
break;
case R.id.seven:
num(7);
break;
case R.id.eight:
num(8);
break;
case R.id.nine:
num(9);
break;
case R.id.dot:
dot();
break;
// 执行运算
// 退格功能
case R.id.zhengfu:
backspace();
break;
case R.id.kaifang:
kaifang();
break;
case R.id.pingfang:
pingfang();
break;
case R.id.add:
add();
break;
case R.id.subtract:
sub();
break;
case R.id.multiply:
multiply();
break;
case R.id.divide:
divide();
break;
case R.id.clear:
clear();
break;
// 计算结果
case R.id.result:
result();
if (!errBoolean && flagBoolean) {
output.setText(String.valueOf(result));
}
default:
break;
}
// if(strold==""&&act==' '&&str=="")
// {
// input.setText("");
// }
// else {
// input.setText(strold+act+str);
// }
if (act == 'k') {
input.setText(strold);
} else {
input.setText(strold + act + str);
}
if (act == 'p') {
input.setText(strold);
} else {
input.setText(strold + act + str);
}
output.setText(String.valueOf(result));
}
private void kaifang() {
// TODO Auto-generated method stub
if (flagBoolean) {
act = 'k';
strold = str;
// str="";
result();
}
}
private void pingfang() {
// TODO Auto-generated method stub
if (flagBoolean) {
act = 'p';
strold = str;
// str="";
result();
}
}
//退格功能
private void backspace() {
// TODO Auto-generated method stub
String str1=input.getText().toString();
if(input.length()>0){
input.setText(str1.substring(0, str1.length()-1));
}
}
private void dot() {
// TODO Auto-generated method stub
if (!flagDot) {
str = str + ".";
flagBoolean = false;
flagDot = true;
}
// str=str+i;
// flagBoolean=false;
}
//清空
private void clear() {
// TODO Auto-generated method stub
str = strold = "";
count = 0;
act = ' ';
result = 0.0;
flagBoolean = false;
flagDot = false;
input.setText(strold + act + str);
output.setText("");
}
private void divide() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '/';
flagBoolean = false;
}
}
private void multiply() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '*';
flagBoolean = false;
}
}
private void sub() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '-';
flagBoolean = false;
}
}
private void add() {
// TODO Auto-generated method stub
if (flagBoolean) {
check();
act = '+';
flagBoolean = false;
}
}
private void check() {
// TODO Auto-generated method stub
if (count >= 1) {
result();
str = String.valueOf(result);
}
strold = str;
str = "";
count++;
flagDot = false;
}
// 计算输出结果
private void result() {
// TODO Auto-generated method stub
if (flagBoolean) {
Double a, b;
// if(act=='k'||act=='c')
// {
// a=Double.parseDouble(strold);
// }
// else {
// a=Double.parseDouble(strold);
// b=Double.parseDouble(str);
// }
a = Double.parseDouble(strold);
b = Double.parseDouble(str);
if (b == 0 && act == '/') {
clear();
output.setText("除数不能为零!");
errBoolean = true;
}
if (!errBoolean) {
switch (act) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
case 'k':
result = Math.sqrt(a);
break;
case 'p':
result = Math.pow(a,2);
break;
default:
break;
}
}
}
}
private void num(int i) {
// TODO Auto-generated method stub
str = str + String.valueOf(i);
flagBoolean = true;
}
}
}
}