欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

android計算器實現(xiàn)兩位數(shù)的加減乘除

本文實例為大家分享了android計算器實現(xiàn)加減乘除的具體代碼,供大家參考,具體內容如下

創(chuàng)新互聯(lián)公司2013年至今,先為廣陵等服務建站,廣陵等地企業(yè),進行企業(yè)商務咨詢服務。為廣陵企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

注:以下計算器只注重實現(xiàn)功能,不考慮其他BUG,只有兩位整數(shù)的算法運算,適合新手

1、實現(xiàn)思想

將從鍵盤得到的數(shù)值放在一個字符數(shù)組中,以運算符號(+-/)為分割點,將兩個數(shù)值分割開,進行算法運算。*

2、難點

如何判斷是否為符號?+ - ×/
記錄符號的位置?

3、步驟:

1、得到鍵盤輸入的值
2、將值存放在一個字符數(shù)組中
3、遍歷數(shù)組中的每個數(shù),如果找到算法符號,記錄下算法符號的位置。(要點,從0開始)
4、將算法符號前面的數(shù)放在一個定義的int型數(shù)中
5、同理
6、判斷是加減乘除的哪一個方法,然后進行簡單的運算。

4、代碼

i:布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:weightSum="1"
 >

 <TextView android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/etResult"
 android:layout_weight="0.05"
 android:textSize="25dp"
 android:paddingTop="10dp"
 android:gravity="bottom"
 android:hint="0.0"
 />
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:layout_weight="0.8">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:weightSum="1">

 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="C"
 android:textSize="25dp"
 android:background="@color/colorWhite"
 android:id="@+id/btnQingchu"
 android:layout_weight="0.5" />
 <Button
 android:layout_width="235dp"
 android:layout_height="wrap_content"
 android:text="←"
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:id="@+id/btnHuishan"
 android:layout_weight="0.5"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn7"
 android:text="7"
 android:textSize="25dp"
 android:layout_weight="0.25"
 />
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn8"
 android:text="8"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn9"
 android:text="9"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnChu"
 android:text="÷"
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:layout_weight="0.25"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn4"
 android:text="4"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn5"
 android:text="5"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn6"
 android:text="6"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnCheng"
 android:text="×"
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:layout_weight="0.25"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn1"
 android:text="1"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn2"
 android:text="2"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn3"
 android:text="3"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnJian"
 android:text="-"
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:layout_weight="0.25"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btn0"
 android:text="0"
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnDian"
 android:text="."
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnDengyu"
 android:text="="
 android:textSize="25dp"
 android:layout_weight="0.25"/>
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/btnJia"
 android:text="+"
 android:textSize="25dp"
 android:background="@color/colorBlue"
 android:layout_weight="0.25"/>
 </LinearLayout>
 </LinearLayout>
</LinearLayout>

ii:獲取鍵盤的值,寫監(jiān)聽

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);
 //綁定監(jiān)聽
 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()){
 //數(shù)字按鈕
 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:
 /* if (b_Clean)
 {
  b_Clean =false;
  etGet.setText("");
 }*/
 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:
 /* if (b_Clean)
 {
  b_Clean =false;
  etGet.setText("");
 }*/
 etGet.setText(str+((Button)v).getText());
 break;
 //清除按鈕
 case R.id.btnQingchu:
 /* if (b_Clean)
 {
  b_Clean =false;
  etGet.setText("");
 }*/
 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:算法功能實現(xiàn)

public void getResult(){
 str = etGet.getText().toString();
 strArray = new String[str.length()]; //將得到的字符串放在一個字符數(shù)組里
 //System.out.println("str"+str);
 int n=0;
 for(int i=0; i<strArray.length;i++){
 strArray[i] = str.substring(i, i+1); //遍歷數(shù)組的每個元素
 //System.out.print(strArray[i]);
 if(strArray[i].equals("+")||strArray[i].equals("-") //滿足條件
  ||strArray[i].equals("×")||strArray[i].equals("÷"))
 {
 n= i; //記錄符號存在的位置
 }
 }
 int num1 = Integer.parseInt(str.substring(0,n)); //得到前一串數(shù)
 String caculate = str.substring(n,n+1); //得到算法符號,加減乘除
 int num2 = Integer.parseInt(str.substring(n+1)); //得到后一串數(shù)

 if (caculate.equals("+"))
 {
 Inputresult = num1+num2;
 }
 else if (caculate.equals("-"))
 {
 Inputresult = num1-num2;
 }
 else if (caculate.equals("×"))
 {
 Inputresult = num1*num2;
 }
 else if (caculate.equals("÷"))
 {
 if (num2==0)
 {
 return ;
 }
 Inputresult = num1/num2;
 }
 etGet.setText(num1+caculate+num2+"="+Inputresult);
 }

更多計算器功能實現(xiàn),請點擊專題: 計算器功能匯總 進行學習

關于Android計算器功能的實現(xiàn),查看專題:Android計算器 進行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

新聞名稱:android計算器實現(xiàn)兩位數(shù)的加減乘除
轉載來源:http://aaarwkj.com/article42/pcdoec.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)網(wǎng)站改版、定制網(wǎng)站、網(wǎng)站制作、企業(yè)網(wǎng)站制作自適應網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營
av中文字幕一二三区| 成人黄色大片免费看| 91在线看片国产免费观看| 午夜av在线毛片免费观看| 亚洲欧美一区二区色慰| 亚洲少妇午夜福利视频| 朋友的尤物人妻中文字幕| 国产91白丝在线观看| 午夜av一区二区三区| av中文资源在线观看| 亚洲成人日韩国产欧美| 国产亚洲精品一区久久| 91欧美在线激情视频| 国产黄色av网站在线| 欧美日韩亚洲国产专区精品| 日本国产一区二区三区在线观看| 激情综合五月激情综合| 日韩精品在线播放观看| 歪歪私人影院午夜毛片| 国产精品久久久在线视频| 国产美女主播一二三区| 中文字幕精品高清中国| 免费日韩黄片在线观看| 国产自偷一区二区三区| 视频久久这里只有精品| 深夜十八禁在线免费观看| 精品日韩电影在线观看| 久久婷亚洲综合五月天| 91精品人妻二区三区| 欧美一区二区三区情色| 18岁以下禁看视频网站| 欧美国产日韩亚洲综合| 国产精品一区欧美精品| 顶级少妇做爰片高潮丸| 素人人妻一区二区三区| 国产在线精品成人欧美| 日日狠狠久久偷偷综合色| 关于男女性生活的视频| 二区三区在线欧美日韩| 你懂的免费视频中文字幕| 国产精品一区二区三区激情|