栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

android计算器实现两位数的加减乘除

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android计算器实现两位数的加减乘除

本文实例为大家分享了android计算器实现加减乘除的具体代码,供大家参考,具体内容如下

注:以下计算器只注重实现功能,不考虑其他BUG,只有两位整数的算法运算,适合新手

1、实现思想

将从键盘得到的数值放在一个字符数组中,以运算符号(+-/)为分割点,将两个数值分割开,进行算法运算。*

2、难点

如何判断是否为符号?+ - ×/
记录符号的位置?

3、步骤:

1、得到键盘输入的值
2、将值存放在一个字符数组中
3、遍历数组中的每个数,如果找到算法符号,记录下算法符号的位置。(要点,从0开始)
4、将算法符号前面的数放在一个定义的int型数中
5、同理
6、判断是加减乘除的哪一个方法,然后进行简单的运算。

4、代码

i:布局:




 
 
 

 

ii:获取键盘的值,写监听

public void getButton(){
 //获取按钮组件
 btn0= (Button) findViewById(R.id.btn0);
 btn1= (Button) findViewById(R.id.btn1);
 btn2= (Button) findViewById(R.id.btn2);
 btn3= (Button) findViewById(R.id.btn3);
 btn4= (Button) findViewById(R.id.btn4);
 btn5= (Button) findViewById(R.id.btn5);
 btn6= (Button) findViewById(R.id.btn6);
 btn7= (Button) findViewById(R.id.btn7);
 btn8= (Button) findViewById(R.id.btn8);
 btn9= (Button) findViewById(R.id.btn9);

 btnJia= (Button) findViewById(R.id.btnJia);
 btnJian= (Button) findViewById(R.id.btnJian);
 btnCheng= (Button) findViewById(R.id.btnCheng);
 btnChu= (Button) findViewById(R.id.btnChu);

 btnDian= (Button) findViewById(R.id.btnDian);
 btnDengyu= (Button) findViewById(R.id.btnDengyu);
 btnQingchu= (Button) findViewById(R.id.btnQingchu);
 btnHuishan= (Button) findViewById(R.id.btnHuishan);

 etGet = (TextView) findViewById(R.id.etResult);
 //绑定监听
 btn0.setonClickListener(this);
 btn1.setonClickListener(this);
 btn2.setonClickListener(this);
 btn3.setonClickListener(this);
 btn4.setonClickListener(this);
 btn5.setonClickListener(this);
 btn6.setonClickListener(this);
 btn7.setonClickListener(this);
 btn8.setonClickListener(this);
 btn9.setonClickListener(this);

 btnJia.setonClickListener(this);
 btnJian.setonClickListener(this);
 btnCheng.setonClickListener(this);
 btnChu.setonClickListener(this);

 btnDian.setonClickListener(this);
 btnDengyu.setonClickListener(this);
 btnQingchu.setonClickListener(this);
 btnHuishan.setonClickListener(this);
 }

iii:绑定按钮

 @Override
 public void onClick(View v) {
 str = etGet.getText().toString();
 switch (v.getId()){
 //数字按钮
 case R.id.btn0:
 case R.id.btn1:
 case R.id.btn2:
 case R.id.btn3:
 case R.id.btn4:
 case R.id.btn5:
 case R.id.btn6:
 case R.id.btn7:
 case R.id.btn8:
 case R.id.btn9:
 
 etGet.setText(str+((Button)v).getText());
 break;
 //运算按钮
 case R.id.btnJia:
 case R.id.btnJian:
 case R.id.btnCheng:
 case R.id.btnChu:
 case R.id.btnDian:
 
 etGet.setText(str+((Button)v).getText());
 break;
 //清除按钮
 case R.id.btnQingchu:
 
 etGet.setText("");
 break;
 case R.id.btnDengyu:
 getResult();
 break;
 case R.id.btnHuishan:
 str=etGet.getText().toString();
 try {
  etGet.setText(str.substring(0,str.length()-1));
 }
 catch (Exception e){
  etGet.setText("");
 }

 break;

 }
 }

iV:算法功能实现

public void getResult(){
 str = etGet.getText().toString();
 strArray = new String[str.length()]; //将得到的字符串放在一个字符数组里
 //System.out.println("str"+str);
 int n=0;
 for(int i=0; i
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/154253.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号