加減不管用,不管輸入什么結果都為0
package com.af.calculatordemo;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
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;
import android.os.Build;
public class MainActivity extends Activity? implements OnClickListener{
??? Button bt_0;//0數字按鈕
??? Button bt_1;//1數字按鈕
??? Button bt_2;//2數字按鈕
??? Button bt_3;//3數字按鈕
??? Button bt_4;//4數字按鈕
??? Button bt_5;//5數字按鈕
??? Button bt_6;//6數字按鈕
??? Button bt_7;//7數字按鈕
??? Button bt_8;//8數字按鈕
??? Button bt_9;//9數字按鈕
??? Button bt_point;//小數點按鈕
??? Button bt_clear;//清除按鈕
??? Button bt_plus;//加號按鈕
??? Button bt_minus;//減號按鈕
??? Button bt_multiply;//乘號按鈕
??? Button bt_divide;//除號按鈕
??? Button bt_equle;//等號按鈕
??? Button bt_del;//刪除按鈕
??? EditText et_input;//顯示輸入內容的顯示屏
??? boolean clear_flag;//清空標識
??? protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_main);
??????? bt_0=(Button) findViewById(R.id.bt_0);
??????? bt_1=(Button) findViewById(R.id.bt_1);
??????? bt_2=(Button) findViewById(R.id.bt_2);
??????? bt_3=(Button) findViewById(R.id.bt_3);
??????? bt_4=(Button) findViewById(R.id.bt_4);
??????? bt_5=(Button) findViewById(R.id.bt_5);
??????? bt_6=(Button) findViewById(R.id.bt_6);
??????? bt_7=(Button) findViewById(R.id.bt_7);
??????? bt_8=(Button) findViewById(R.id.bt_8);
??????? bt_9=(Button) findViewById(R.id.bt_9);
??????? bt_point=(Button) findViewById(R.id.bt_point);
??????? bt_clear=(Button) findViewById(R.id.bt_clear);
??????? bt_plus=(Button) findViewById(R.id.bt_plus);
??????? bt_minus=(Button) findViewById(R.id.bt_minus);
??????? bt_multiply=(Button) findViewById(R.id.bt_multiply);
??????? bt_divide=(Button) findViewById(R.id.bt_divide);
??????? bt_equle=(Button) findViewById(R.id.bt_equle);
??????? bt_del=(Button) findViewById(R.id.bt_del);
????? ?
??????? //以上是實例化按鈕
?????? ?
??????? et_input=(EditText) findViewById(R.id.et_input);
??????? //實例化輸入之后的顯示屏
?????? ?
?????? //給按鈕實現點擊事件
??????? bt_0.setOnClickListener(this);
??????? bt_1.setOnClickListener(this);
??????? bt_2.setOnClickListener(this);
??????? bt_3.setOnClickListener(this);
??????? bt_4.setOnClickListener(this);
??????? bt_5.setOnClickListener(this);
??????? bt_6.setOnClickListener(this);
??????? bt_7.setOnClickListener(this);
??????? bt_8.setOnClickListener(this);
??????? bt_9.setOnClickListener(this);
??????? bt_point.setOnClickListener(this);
??????? bt_clear.setOnClickListener(this);
??????? bt_plus.setOnClickListener(this);
??????? bt_minus.setOnClickListener(this);
??????? bt_multiply.setOnClickListener(this);
??????? bt_divide.setOnClickListener(this);
??????? bt_equle.setOnClickListener(this);
??????? bt_del.setOnClickListener(this);
??????? et_input.setOnClickListener(this);
????? ?
??? }
?? ?@Override
?? ?public void onClick(View v) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?String str=et_input.getText().toString();
?? ??? ?switch (v.getId()) {
?? ??? ?case R.id.bt_0:
?? ??? ?case R.id.bt_1:
?? ??? ?case R.id.bt_2:
?? ??? ?case R.id.bt_3:
?? ??? ?case R.id.bt_4:
?? ??? ?case R.id.bt_5:
?? ??? ?case R.id.bt_6:
?? ??? ?case R.id.bt_7:
?? ??? ?case R.id.bt_8:
?? ??? ?case R.id.bt_9:
?? ??? ?case R.id.bt_point:
?? ??? ??? ?if(clear_flag){
?? ??? ??? ??? ?clear_flag=false;
?? ??? ??? ??? ?str="";
?? ??? ??? ??? ?et_input.setText("");
?? ??? ??? ?}
?? ??? ??? ?et_input.setText(str+((Button)v).getText());
?? ??? ??? ?break;
?? ??? ?case R.id.bt_plus:
?? ??? ?case R.id.bt_minus:
?? ??? ?case R.id.bt_multiply:
?? ??? ?case R.id.bt_divide:
?? ??? ??? ?if(clear_flag){
?? ??? ??? ??? ?clear_flag=false;
?? ??? ??? ??? ?str="";
?? ??? ??? ??? ?et_input.setText("");
?? ??? ??? ?}
?? ??? ??? ?et_input.setText(str+" "+((Button)v).getText()+" ");
?? ??? ??? ?break;
?? ??? ?case R.id.bt_clear:
?? ??? ??? ?
?? ??? ??? ??? ?clear_flag=false;
?? ??? ??? ??? ?str="";
?? ??? ??? ?
?? ??? ??? ?et_input.setText("");
?? ??? ??? ?break;
?? ??? ?case R.id.bt_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.bt_equle:
?? ??? ??? ?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,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.equals(".")&&!s2.equals(".")&&!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.equals(".")){
?? ??? ??? ??? ?int r =(int) result;
?? ??? ??? ??? ?et_input.setText(r+"");
?? ?
?? ??? ??? ?}else{
?? ??? ??? ??? ?et_input.setText(result+"");
?? ??? ?
?? ??? ??? ?}
?? ????? }else{
?? ??? ??? ? et_input.setText("");
?? ?}
?? ?}
}
2017-07-23
第一:應該是你的運行環境或者說你的模擬器有問題。你的代碼在我的eclipse上沒有編譯錯誤,在模擬器上運行也正常使用,推薦你使用這種方法運行程序? http://jingyan.baidu.com/article/066074d637ef16c3c21cb0b4.html? 下一個模擬器,速度快,效果也好。
第二:你的代碼,下劃線部分,op.equals(“里面的運算符號”) 一定要和activity_main的Button里的android:text="運算符號"保持一致!
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.equals(".")){
?? ??? ??? ??? ?int r =(int) result;
?? ??? ??? ??? ?et_input.setText(r+"");
?? ?
?? ??? ??? ?}else{
?? ??? ??? ??? ?et_input.setText(result+"");
?? ??? ?
?? ??? ??? ?}
?? ????? }else{
?? ??? ??? ? et_input.setText("");
?? ?}
2018-08-18
java才