如何在计算器程序中添加第二个参数(数字)



我正在Unity中开发一个小型计算器程序。我只需要计算器就可以计算两个数字。

我试图实现的功能:输入数学运算符后,它应该显示第三个索引中的第二个数字。

问题:如果在键盘上按下不同的数字,第一个数字将被覆盖,而不是添加第二个数字。

这是我创建的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Functions : MonoBehaviour
{
// Global Variable to display text on top panel
public Text panelText;
// Create a number variable
string num;
string num1;
string num2;
string mOpr;
string calNum;
string cbutton;
string opr;
bool isFirstNum;
// Start is called before the first frame update
void Start()
{
}

// A function with an int argument
public void NumberInputOne(string num)
{
num1 = num;
num2 = num;
if (panelText.text.Length < 1)
{
Debug.Log(num1);
panelText.text = num1;
isFirstNum = false;
}
else if (panelText.text.Length > 1 && panelText.text.Length < 3)
{
num2 = num;
Debug.Log(num2);
panelText.text = num1 + mOpr + num2;
}
}
public void OperatorInput(string opr)
{
mOpr = opr;
if (panelText.text.Length > 0 && panelText.text.Length < 2)
{
panelText.text = num1 + mOpr;
}
}
// public void NumberInputTwo(int num)
//{
//    ResNum2 = num;
//    Debug.Log(ResNum2);
//    if (panelText.text.Length > 1 && panelText.text.Length < 3)
//    {
//        panelText.text = ResNum1 + opr + ResNum2;
//    }
// }
public void RestartCal(string cButton)
{
panelText.text = "";
}
}

我还添加了一个屏幕记录来捕捉问题:

覆盖的第一个号码

你有什么建议吗?

谢谢

使用NumberInputOne函数,如下所示;

public void NumberInputOne(string num)
{
if (num1 is null)
{
Debug.Log(num1);
panelText.text = num1;
num1 = num
}
else
{
num2 = num;
Debug.Log(num2);
panelText.text = num1 + mOpr + num2;
}
}

顺便说一句,我建议您查看示例计算应用程序代码。因为除了你的要求之外,总的来说还有一些地方需要改进。

这感觉像是一个初学者编程练习。但是,构建计算器的正确方法涉及到编程概念,而这些概念可能还没有教过你。这使得这是一个糟糕的选择作为一项任务。

就我个人而言,我会通过定义一个简单的语法树来表示输入的公式来构建一个计算器。包括显示公式和计算答案的方法。例如

public interface IValue
{
int Calculate();
string PrintValue();
}
public class Number : IValue
{
public int? Value;
public void AddDigit(int digit) => Value = (Value ?? 0) * 10 + digit;
public int Calculate() => Value ?? 0;
public string PrintValue() => Value?.ToString();
}
public abstract class BinaryOperator : IValue
{
public IValue Left;
public IValue Right;
public abstract int Operation(int left, int right);
public abstract char Operator { get; }
public int Calculate()
{
var left = Left.Calculate();
var right = Right.Calculate();
return Operation(left, right);
}
public string PrintValue() => $"{Left?.PrintValue()} {Operator} {Right?.PrintValue()}";
}
public class Addition : BinaryOperator
{
public override char Operator => '+';
public override int Operation(int left, int right) => left + right;
}
// TODO define other operators

然后思考每个按钮应该如何更改语法树。

// the entire formula
public IValue Root;
// the number currently being typed
public Number Input;
public void Display() {
panelText.text = Root.PrintValue();
}
// start / clear
public void Start(){
Root = Input = new Number(){
Value = 0
};
Display();
}
public void Plus(){
// left as an exercise for the reader
Display();
}
public void Digit(int digit) {
Input.AddDigit(digit);
Display();
}
public void Calculate() {
// left as an exercise for the reader
Display();
}

相关内容

最新更新