溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

unity怎么實(shí)現(xiàn)簡單計算器

發(fā)布時間:2021-08-09 02:14:36 來源:億速云 閱讀:164 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“unity怎么實(shí)現(xiàn)簡單計算器”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

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

using System.Text;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System;

public class Calculator : MonoBehaviour
{
    public Text SpendText;
    private StringBuilder spendPrice;//初始金額
    private string rmbSymbol;
    private float totalPrice, spendPrices;//總和,初始金額
    private bool isFirstDecrease;//避免減為零后的第二次起不能為負(fù)
    private bool? isPlusOrDecrease, countType;//點(diǎn)擊加減符號,點(diǎn)擊等號
    public Button PointButton; 
    private int count;//限制最大輸入數(shù)
    private void Start()
    {
        spendPrice = new StringBuilder();
        totalPrice = 0;
        spendPrices = 0;
        rmbSymbol = "<size='50'>&yen;</size> ";
        isPlusOrDecrease = null;//true為加,false為減
        countType = null;//true為加,false為減
        isFirstDecrease = true;
        count = 0;
    }

    public void PointButtonController(bool type)
    {
        PointButton.interactable = type;
    }

    public void InputNumber(int num)
    {
        //按鈕
        switch (num)
        {
            case 0:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("0");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 1:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("1");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 2:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("2");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 3:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("3");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 4:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("4");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 5:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("5");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 6:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("6");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 7:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("7");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 8:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("8");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 9:
                if (count < 11)
                {
                    count++;
                    spendPrice.Append("9");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 10:
                if (count < 11)
                {
                    count += 2;
                    spendPrice.Append("00");
                    SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                }
                break;
            case 11://加
                isPlusOrDecrease = true;
                countType = true;
                count = 0;
                if (!spendPrice.ToString().Equals(""))
                {
                    if ((totalPrice + float.Parse(spendPrice.ToString()) < 99999999999) && !totalPrice.ToString().Contains("E"))
                    {
                        
                        PointButtonController(true);
                        if (totalPrice != 0)//避免第一次點(diǎn)擊加號時沒反應(yīng)
                        {
                            TotalCount();
                        }
                        CountNum();
                    }
                    else
                    {
                        count = 0;
                        PointButtonController(true);
                        totalPrice = 0;
                        spendPrice.Clear();
                        SpendText.DOText(rmbSymbol, 0);
                        isFirstDecrease = true;
                    }
                }
                break;
            case 12://減
                isPlusOrDecrease = false;
                countType = false;
                count = 0;
                if (!spendPrice.ToString().Equals(""))
                {
                    if ((totalPrice + float.Parse(spendPrice.ToString()) < 99999999999) && !totalPrice.ToString().Contains("E"))
                    {
                        PointButtonController(true);
                        if (totalPrice != 0)//避免第一次點(diǎn)擊減號時沒反應(yīng)
                        {
                            TotalCount();
                        }
                        CountNum();
                    }
                    else
                    {
                        count = 0;
                        PointButtonController(true);
                        totalPrice = 0;
                        spendPrice.Clear();
                        SpendText.DOText(rmbSymbol, 0);
                        isFirstDecrease = true;
                    }
                }
                break;
            case 13://點(diǎn)
                PointButtonController(false);
                spendPrice.Append(".");
                SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
                break;
            case 14://等號
                count = 0;
                if (!spendPrice.ToString().Equals(""))
                {
                    if ((totalPrice + float.Parse(spendPrice.ToString()) < 9999999999) && !totalPrice.ToString().Contains("E"))
                    {
                        PointButtonController(true);
                        TotalCount();
                    }
                    else
                    {
                        count = 0;
                        PointButtonController(true);
                        totalPrice = 0;
                        spendPrice.Clear();
                        SpendText.DOText(rmbSymbol, 0);
                        isFirstDecrease = true;
                    }
                }
                break;
            case 15://清零
                count = 0;
                PointButtonController(true);
                totalPrice = 0;
                spendPrice.Clear();
                SpendText.DOText(rmbSymbol, 0);
                isFirstDecrease = true;
                break;
            default:
                break;
        }
    }
    public void CountNum()
    {
        if (spendPrice.ToString().StartsWith("0") || spendPrice.ToString().Equals(""))//去除開始的無效零
        {
            if (spendPrice.ToString().TrimStart('0') == "" || spendPrice.ToString().TrimStart('0').TrimEnd('0') == ".")//0000,00.00,0.,.0
            {
                spendPrices = 0;
            }
            else
            {
                spendPrices = float.Parse((float.Parse(spendPrice.ToString().TrimStart('0'))).ToString("f2"));
            }
        }
        else
        {
            spendPrices = float.Parse((float.Parse(spendPrice.ToString())).ToString("f2"));
        }
        if (isPlusOrDecrease == true && totalPrice != 0 && spendPrices != 0)
        {
            totalPrice += spendPrices;
            spendPrice.Clear();
            SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
            isPlusOrDecrease = null;
        }
        else if (isPlusOrDecrease == true && totalPrice == 0 && spendPrices != 0 && spendPrices != 0)
        {
            totalPrice = spendPrices;
            spendPrice.Clear();
        }
        
        if (isPlusOrDecrease == false && totalPrice == 0 && spendPrices != 0 && isFirstDecrease)
        {
            totalPrice = spendPrices;
            spendPrice.Clear();
            isFirstDecrease = false;
        }
        else if (isPlusOrDecrease == false && spendPrices != 0)
        {
            totalPrice -= spendPrices;
            spendPrice.Clear();
            SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
            isPlusOrDecrease = null;
        }
    }

    public void TotalCount()
    {
        if (spendPrice.ToString().StartsWith("0") || spendPrice.ToString().Equals(""))
        {
            if (spendPrice.ToString().TrimStart('0') == "" || spendPrice.ToString().TrimStart('0').TrimEnd('0') == ".")
            {
                spendPrices = 0;
            }
            else
            {
                spendPrices = float.Parse((float.Parse(spendPrice.ToString().TrimStart('0'))).ToString("f2"));
            }
        }
        else
        {
            spendPrices = float.Parse((float.Parse(spendPrice.ToString())).ToString("f2"));
        }
        if (spendPrices != 0)
        {
            if (countType == true)
            {
                totalPrice += spendPrices;
            }
            else if (countType == false)
            {
                totalPrice -= spendPrices;
            }
            spendPrice.Clear();
            SpendText.DOText(rmbSymbol + totalPrice.ToString(), 0.1f);
            countType = null;
        }
    }
    //將科學(xué)計數(shù)法轉(zhuǎn)化為普通數(shù)字
    private Decimal ChangeDataToD(string strData)
    {
        Decimal dData = 0.0M;
        if (strData.Contains("E"))
        {
            dData = Decimal.Parse(strData, System.Globalization.NumberStyles.Float);
        }
        return dData;
    }
}

unity怎么實(shí)現(xiàn)簡單計算器

“unity怎么實(shí)現(xiàn)簡單計算器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI