sinat_24266165 2014-12-06 07:45 采纳率: 0%
浏览 1728

android 简单计算机关于算法问题

package com.example.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{  
    public float result; // 计算结果    
    TextView showResult;//显示计算的结果  
    Button[] number;//数字数字包含点  
    Button[] command;//符号数组  
    Button ClearButton;//操作按钮  

    public String lastCommand; // 用于保存运算符     
    public boolean clearFlag; // 用于判断是否清空显示区域的值,true需要,false不需要     
    public boolean firstFlag; // 用于判断是否是首次输入,true首次,false不是首次    
    public MainActivity(){//初始化变量  
        result=0;  
        clearFlag=false;  
        firstFlag=true;  
        lastCommand="=";}  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        showResult=(TextView)findViewById(R.id.result);//实例化显示计算结果TextView  

        number=new Button[11];  
        number[0]=(Button)findViewById(R.id.ling);  
        number[1]=(Button)findViewById(R.id.yi);  
        number[2]=(Button)findViewById(R.id.er);  
        number[3]=(Button)findViewById(R.id.san);  
        number[4]=(Button)findViewById(R.id.si);  
        number[5]=(Button)findViewById(R.id.wu);  
        number[6]=(Button)findViewById(R.id.liu);  
        number[7]=(Button)findViewById(R.id.qi);  
        number[8]=(Button)findViewById(R.id.ba);  
        number[9]=(Button)findViewById(R.id.jiu);  
        number[10]=(Button)findViewById(R.id.dian);  
        //运算符数组  
        command=new Button[5];  
        command[0]=(Button)findViewById(R.id.jia);  
        command[1]=(Button)findViewById(R.id.jian);  
        command[2]=(Button)findViewById(R.id.cheng);  
        command[3]=(Button)findViewById(R.id.chu);  
        command[4]=(Button)findViewById(R.id.deng);  
        //为每个数字按钮注册按钮单击事件  
        NumberAction na=new NumberAction();  
        for(Button bc:number){  
            bc.setOnClickListener(na);}  
        //为每个操作符号按钮注册按钮单击事件  
        CommandAction ca=new CommandAction();  
        for(Button bc:command){  
            bc.setOnClickListener(ca);}  
        //实例化按钮  
        ClearButton=(Button)findViewById(R.id.clear);  

        //为按钮注册单击事件  
        ClearButton.setOnClickListener(this);  

    }  
    public void calculate(double x){ //计算函数  
        if(lastCommand.equals("+"))  
        {  
            result+=x;  
        }  
        else if(lastCommand.equals("-"))  
        {  
            result-=x;  
        }  
        else if(lastCommand.equals("*"))  
        {  
            result*=x;  
        }  
        else if(lastCommand.equals("/"))  
        {  
            result/=x;  
        }  
        showResult.setText(""+result);  
    }  
    public void onClick(View v) {//按钮单击处理  
        switch(v.getId()){  
        case R.id.clear://当单击了Clear按钮后  
            showResult.setText("0.0");  
            result=0;  
            clearFlag=false;  
            firstFlag=true;  
            lastCommand="=";  
            break;  
        }  
    }  
    private class NumberAction implements OnClickListener{//数字事件注册类  
        public void onClick(View v) {  
            Button btn=(Button)v;  
            String input=btn.getText().toString();//取得单击该按钮的值  
            if(firstFlag){//如果是第一次输入  
                if(input.equals(".")){//判断如果第一次输入是".",则不做任何事  
                    return;  
                }  
                if(showResult.getText().toString().equals("0.0")){  
                    showResult.setText("");  
                }  
                result=Float.parseFloat(input);  
                firstFlag=false;  
            }  
            else{//如果不是第一次输入  
                String text=showResult.getText().toString();//取得先前的内容  
                //判断内容中是否有“.”,如果有,下一步输入的又是".",则不做什么  
                if(text.indexOf(".")!=-1 && input.equals(".")){  
                    return;  
                }  
                //判断内容是不是等于"=",如果是,并且即将输入的是".",则不允许  
                if(text.equals("-") && input.equals(".")){  
                    return;  
                }  
                //判断内容是不是等于"=",如果是,并且即将输入的不是".",则把输入的值赋给result,比如result=-6;  
                if(text.equals("-") && !input.equals(".")){  
                    result=Float.parseFloat("-"+input);  
                }  
                //判断内容是不是等于"0",如果是,并且即将输入的不是".",则不允许  
                if(text.equals("0") && !input.equals(".")){  
                    return;  
                }  
            }  
            if(clearFlag){  
                showResult.setText("");  
                clearFlag=false;  
            }  
            showResult.setText(showResult.getText().toString()+input);//显示内容  
        }  
    }  
    private class CommandAction implements OnClickListener{//符号操作等按钮的注册事件  
        public void onClick(View v) {  
            Button btn=(Button)v;  
            String inputCommand=(String)btn.getText();//取得按钮上的值  
            if(firstFlag){//判断是否是第一次输入  
                if(inputCommand.equals("-")){//如果是,如果输入的是"-"  
                    showResult.setText("-");//则把showResult内容设置为"-"  
                    firstFlag=false;  
                }  
            }  
            else{//如果不是第一次输入  
                if(!clearFlag){  
                    calculate(Double.parseDouble(showResult.getText().toString()));  
                }  
                lastCommand=inputCommand;  
                clearFlag=true;  
            }  
        }  
    }  
}  





每次第一次输入一个两位数以上的数字进行计算,计算机都只识别第一位输入的数字
请问这段短代码应该怎么修改好?
  • 写回答

1条回答 默认 最新

  • 123mimosa 2014-12-06 09:28
    关注

    这样写就只能识别第一个数,因为只读取了当前按钮的值,之后的就没有处理程序啦。
    比如23*20=需要判断输入数字2之后是否继续是数字,如果是就需要将两个数字合并,以运算符或点号为界限截断处理,小数点以前和以后的处理不一样,之前为每读一位乘十的n-1次幂,试一下吧

    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型