带记忆按钮的C#计算器
我有一些困难,使我的计算器有能力存储值。除了这个,计算器什么都能用,我被卡住了。我想我可能必须声明一些常量或我现在缺少的东西。我在这方面是个新手,非常感谢你的帮助。这是我的密码。谢谢你们的帮助。现在我没有收到任何错误,但也没有任何效果!我也应该这样做,当有一个值存储在内存中时,文本框中会出现一个“M”,但我认为从这一部分开始更容易带记忆按钮的C#计算器,c#,memory,calculator,C#,Memory,Calculator,我有一些困难,使我的计算器有能力存储值。除了这个,计算器什么都能用,我被卡住了。我想我可能必须声明一些常量或我现在缺少的东西。我在这方面是个新手,非常感谢你的帮助。这是我的密码。谢谢你们的帮助。现在我没有收到任何错误,但也没有任何效果!我也应该这样做,当有一个值存储在内存中时,文本框中会出现一个“M”,但我认为从这一部分开始更容易 private void digitCalculate_Click(object sender, EventArgs e) { Button
private void digitCalculate_Click(object sender, EventArgs e)
{
Button ButtonThatWasPushed = (Button)sender;
string ButtonText = ButtonThatWasPushed.Text;
decimal EndResult = 0;
decimal MemoryStore = 0;
if (ButtonText == "MC")
{
//Memory Clear
MemoryStore = 0;
return;
}
if (ButtonText == "MR")
{
//Memory Recall
txtDisplay.Text = MemoryStore.ToString();
return;
}
if (ButtonText == "MS")
{
// Memory subtract
MemoryStore -= EndResult;
return;
}
if (ButtonText == "M+")
{
// Memory add
MemoryStore += EndResult;
return;
}
}
对于
decimalmemorystore=0,您需要有表单级变量代码>,因为您有函数级变量,所以当您单击digitCalculate
按钮时,它将初始化为0
decimal MemoryStore = 0;
decimal EndResult = 0;
public Form1()
{
InitializeComponent();
}
private void digitCalculate_Click(object sender, EventArgs e)
{
Button ButtonThatWasPushed = (Button)sender;
string ButtonText = ButtonThatWasPushed.Text;
//decimal EndResult = 0;
//decimal MemoryStore = 0;
还要注意的是
- MC=内存清除将内存设置为0
- MR=记忆调用使用
内存中的数字
- MS=内存存储将显示屏上的数字放入内存
您需要更改“MS”
逻辑并添加“M-”
只需将变量MemoryStore更改为全局变量。目前,每次按下按钮时都会重新声明,这意味着在按下按钮之间数据会丢失。将它移到函数之外,它应该可以正常工作。因此,当我添加该行时,我得到的MemoryStore被赋值,但它的值从未被使用。我还应该将decimal EndResult=0;外面也一样吗?我试过了,但没用。我试过了,又玩了一些。它看起来应该工作,但它就是不…:(我甚至不知道prob可能在哪里。不过谢谢你的帮助!
if (ButtonText == "MS")
{
MemoryStore = Decimal.Parse(txtDisplay.Text);
return;
}
if (ButtonText == "M-")
{
// Memory subtract
MemoryStore -= EndResult;
txtDisplay.Text = MemoryStore.ToString();
return;
}
if (ButtonText == "M+")
{
// Memory add
MemoryStore += EndResult;
txtDisplay.Text = MemoryStore.ToString();
return;
}