亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android?studio實現(xiàn)簡單計算器的編寫

 更新時間:2022年05月20日 15:08:34   作者:Zzq_Fighting  
這篇文章主要為大家詳細介紹了Android?studio實現(xiàn)簡單計算器的編寫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android studio實現(xiàn)簡單計算器的具體代碼,供大家參考,具體內(nèi)容如下

話不多說,首先附上代碼:

MainActivity.java

package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import java.util.Stack;

public class MainActivity extends AppCompatActivity {

? ? EditText edit = null;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? edit = findViewById(R.id.edit_textview);
? ? }

? ? public void btnClick(View view) {
? ? ? ? switch (view.getId()){
? ? ? ? ? ? case R.id.btn0:
? ? ? ? ? ? ? ? edit.append("0");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn1:
? ? ? ? ? ? ? ? edit.append("1");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn2:
? ? ? ? ? ? ? ? edit.append("2");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn3:
? ? ? ? ? ? ? ? edit.append("3");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn4:
? ? ? ? ? ? ? ? edit.append("4");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn5:
? ? ? ? ? ? ? ? edit.append("5");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn6:
? ? ? ? ? ? ? ? edit.append("6");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn7:
? ? ? ? ? ? ? ? edit.append("7");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn8:
? ? ? ? ? ? ? ? edit.append("8");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn9:
? ? ? ? ? ? ? ? edit.append("9");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btnPlus:
? ? ? ? ? ? ? ? edit.append("+");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btnSubtract:
? ? ? ? ? ? ? ? edit.append("-");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btnMultiply:
? ? ? ? ? ? ? ? edit.append("*");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btnDivide:
? ? ? ? ? ? ? ? edit.append("/");
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }

? ? public void btnEqual(View view) {
? ? ? ? String str = edit.getText().toString();//1+2
? ? ? ? String res="";
? ? ? ? //Java計算代碼
? ? ? ? String result = calculate(str);
? ? ? ? edit.setText(result);
? ? }

? ? private static int number(char[] arr,int start,int end){
? ? ? ? StringBuilder buffer = new StringBuilder();
? ? ? ? for(int i=start;i<=end;i++){
? ? ? ? ? ? buffer.append(arr[i]);
? ? ? ? }
? ? ? ? return Integer.parseInt(buffer.toString());
? ? }
? ? // 待實現(xiàn)函數(shù),在此函數(shù)中填入答題代碼
? ? private static int comp(String op){
? ? ? ? if(op.equals("+") || op.equals("-"))
? ? ? ? ? ? return 1;
? ? ? ? if(op.equals("*") || op.equals("/"))
? ? ? ? ? ? return 2;
? ? ? ? return 0;
? ? }
? ? private static String compute(Integer a,Integer b,String op){
? ? ? ? Integer res;
? ? ? ? if(op.equals("+")) {
? ? ? ? ? ? res = a + b;
? ? ? ? ? ? return res.toString();
? ? ? ? }
? ? ? ? if (op.equals("-")) {
? ? ? ? ? ? res= a - b;
? ? ? ? ? ? return res.toString();
? ? ? ? }
? ? ? ? if (op.equals("*")) {
? ? ? ? ? ? res = a * b;
? ? ? ? ? ? return res.toString();
? ? ? ? }
? ? ? ? if (op.equals("/") && b!=0) {
? ? ? ? ? ? res= a / b;
? ? ? ? ? ? return res.toString();
? ? ? ? }else{
? ? ? ? ? ? return "error";
? ? ? ? }
? ? }
? ? private static String calculate(String source) {
? ? ? ? Stack<Integer> numbers=new Stack<>();
? ? ? ? Stack<String> operator=new Stack<>();
? ? ? ? operator.push(".");
? ? ? ? char[] exps=source.toCharArray();
? ? ? ? int start=0;
? ? ? ? if(exps[0]=='-') numbers.push(0);
? ? ? ? for(int j=0;j<exps.length;j++){
? ? ? ? ? ? if(exps[j]=='+' || exps[j]=='*' || exps[j]=='/' || exps[j]=='-'){
? ? ? ? ? ? ? ? if (start <= j - 1) {
? ? ? ? ? ? ? ? ? ? numbers.push(number(exps,start,j-1));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? start=j+1;
? ? ? ? ? ? ? ? while (comp(operator.peek())>=comp(String.valueOf(exps[j]))){
? ? ? ? ? ? ? ? ? ? Integer two=numbers.peek();numbers.pop();
? ? ? ? ? ? ? ? ? ? Integer one=numbers.peek();numbers.pop();
? ? ? ? ? ? ? ? ? ? String result=compute(one,two,operator.peek());operator.pop();
? ? ? ? ? ? ? ? ? ? if (result.equals("error")) {
? ? ? ? ? ? ? ? ? ? ? ? return result;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? numbers.push(Integer.parseInt(result));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? operator.push(String.valueOf(exps[j]));
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? numbers.push(number(exps,start,exps.length-1));
? ? ? ? while (operator.size()>1){
? ? ? ? ? ? Integer two=numbers.peek();numbers.pop();
? ? ? ? ? ? Integer one =numbers.peek();numbers.pop();
? ? ? ? ? ? String op=operator.peek();operator.pop();
? ? ? ? ? ? String value = compute(one, two, op);
? ? ? ? ? ? if (value.equals("error")) {
? ? ? ? ? ? ? ? return value;
? ? ? ? ? ? }
? ? ? ? ? ? numbers.push(Integer.parseInt(value));
? ? ? ? }
? ? ? ? return numbers.peek().toString();
? ? }


? ? public void btnClear(View view) {
? ? ? ? edit.setText("");
? ? }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:app="http://schemas.android.com/apk/res-auto"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical"
? ? tools:context=".MainActivity">

? ? <EditText
? ? ? ? android:id="@+id/edit_textview"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"/>

? ? <TableLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:stretchColumns="0,1,2,3">

? ? ? ? <TableRow>

? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn7"
? ? ? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? ? ? android:onClick="btnClick"
? ? ? ? ? ? ? ? android:text="7" />
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn8"
? ? ? ? ? ? ? ? android:text="8"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn9"
? ? ? ? ? ? ? ? android:text="9"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btnDivide"
? ? ? ? ? ? ? ? android:text="÷"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? </TableRow>

? ? ? ? <TableRow>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn4"
? ? ? ? ? ? ? ? android:text="4"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn5"
? ? ? ? ? ? ? ? android:text="5"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn6"
? ? ? ? ? ? ? ? android:text="6"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btnMultiply"
? ? ? ? ? ? ? ? android:text="×"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? </TableRow>

? ? ? ? <TableRow>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn1"
? ? ? ? ? ? ? ? android:text="1"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn2"
? ? ? ? ? ? ? ? android:text="2"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn3"
? ? ? ? ? ? ? ? android:text="3"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btnSubtract"
? ? ? ? ? ? ? ? android:text="-"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? </TableRow>

? ? ? ? <TableRow>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btnClear"
? ? ? ? ? ? ? ? android:text="C"
? ? ? ? ? ? ? ? android:onClick="btnClear"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btn0"
? ? ? ? ? ? ? ? android:text="0"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btnEqual"
? ? ? ? ? ? ? ? android:text="="
? ? ? ? ? ? ? ? android:onClick="btnEqual"/>
? ? ? ? ? ? <Button
? ? ? ? ? ? ? ? android:id="@+id/btnPlus"
? ? ? ? ? ? ? ? android:text="+"
? ? ? ? ? ? ? ? android:onClick="btnClick"/>
? ? ? ? </TableRow>
? ? </TableLayout>

</LinearLayout>

計算器界面:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android使用Kotlin實現(xiàn)多節(jié)點進度條

    Android使用Kotlin實現(xiàn)多節(jié)點進度條

    這篇文章主要為大家詳細介紹了Android使用Kotlin實現(xiàn)多節(jié)點進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Android 游戲引擎libgdx 資源加載進度百分比顯示案例分析

    Android 游戲引擎libgdx 資源加載進度百分比顯示案例分析

    因為案例比較簡單,所以簡單用AndroidApplication -> Game -> Stage 搭建框架感興趣的朋友可以參考下
    2013-01-01
  • Android Google AutoService框架使用詳解

    Android Google AutoService框架使用詳解

    AutoService是Google開發(fā)一個自動生成SPI清單文件的框架??催^一些基于APT的三方框架源碼的讀者應(yīng)該有所了解。比如Arouter、EventBus等等
    2022-11-11
  • Android實現(xiàn)隱私政策彈窗與鏈接功能

    Android實現(xiàn)隱私政策彈窗與鏈接功能

    現(xiàn)在幾乎所有的應(yīng)用市場都要求應(yīng)用上架需要用戶協(xié)議/隱私政策,本篇內(nèi)容將介紹如何在APP內(nèi)植入一個隱私政策彈窗與鏈接,對Android隱私政策彈窗實現(xiàn)代碼感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • Android實現(xiàn)帶有指示器的進度條

    Android實現(xiàn)帶有指示器的進度條

    這篇文章主要介紹了Android實現(xiàn)帶有指示器的進度條的示例代碼,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-05-05
  • Android11文件管理權(quán)限申請詳細介紹

    Android11文件管理權(quán)限申請詳細介紹

    大家好,本篇文章主要講的是Android11文件管理權(quán)限申請詳細介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • android實現(xiàn)在圖標(biāo)上顯示數(shù)字

    android實現(xiàn)在圖標(biāo)上顯示數(shù)字

    這篇文章主要為大家詳細介紹了android實現(xiàn)在圖標(biāo)上顯示數(shù)字,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • android屏蔽按鈕連續(xù)點擊的示例代碼

    android屏蔽按鈕連續(xù)點擊的示例代碼

    這篇文章主要介紹了android屏蔽按鈕連續(xù)點擊的示例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Flutter 滾動監(jiān)聽及實戰(zhàn)appBar滾動漸變的實現(xiàn)

    Flutter 滾動監(jiān)聽及實戰(zhàn)appBar滾動漸變的實現(xiàn)

    這篇文章主要介紹了Flutter 滾動監(jiān)聽及實戰(zhàn)appBar滾動漸變,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Flutter基本組件Basics?Widget學(xué)習(xí)

    Flutter基本組件Basics?Widget學(xué)習(xí)

    本文詳細講解了Flutter基本組件Basics?Widget,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12

最新評論