gpt4 book ai didi

c# - 对 C# 中的计算器操作感到困惑

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:14 27 4
gpt4 key购买 nike

我正在使用 Windows 窗体应用程序制作一个计算器,但我现在很困惑。到目前为止,这是我的应用程序,它只能做加法,但可以做任意数量的数字。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Form1 : Form
{
int num;
int answer = 0;
int i;


public Form1()
{
InitializeComponent();
}



private void plusButton_Click(object sender, EventArgs e)
{
answer += System.Convert.ToInt32(textBox1.Text);
ClearTextbox();
}

private void minusButton_Click(object sender, EventArgs e)
{
//answer -= System.Convert.ToInt32(textBox1.Text);
//ClearTextbox();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void divideButton_Click(object sender, EventArgs e)
{

}

private void multiplyButton_Click(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
num = 1;
displayInt(num);
}

private void button2_Click(object sender, EventArgs e)
{
num = 2;
displayInt(num);
}

private void button3_Click(object sender, EventArgs e)
{
num = 3;
displayInt(num);
}

private void button4_Click(object sender, EventArgs e)
{
num = 4;
displayInt(num);
}

private void button5_Click(object sender, EventArgs e)
{
num = 5;
displayInt(num);
}

private void button6_Click(object sender, EventArgs e)
{
num = 6;
displayInt(num);
}

private void button7_Click(object sender, EventArgs e)
{
num = 7;
displayInt(num);
}

private void button8_Click(object sender, EventArgs e)
{
num = 8;
displayInt(num);
}

private void button9_Click(object sender, EventArgs e)
{
num = 9;
displayInt(num);
}

private void button0_Click(object sender, EventArgs e)
{
num = 0;
displayInt(num);
}

private void resetButton_Click(object sender, EventArgs e)
{
ClearTextbox();
num = 0;
answer = 0;

}

public void displayInt(int num)
{
textBox1.Text = textBox1.Text + num.ToString();
}

public void ClearTextbox()
{
textBox1.Clear();
}

public void DisplayAnswer(int answer)
{
answer += System.Convert.ToInt32(textBox1.Text); //Answer = Answer + Textbox Stuff


textBox1.Clear();
textBox1.Text = answer.ToString();
}

private void equalsButton_Click(object sender, EventArgs e)
{
DisplayAnswer(answer);
num = 0;
answer = 0;
}


}
}

我不确定是否有办法等到按下另一个数字键,然后执行 x + y。我听说过事件处理程序,但对我来说这是一个相当模糊的话题。

这是一张图片:http://img196.imageshack.us/img196/4127/12464e13e5154a949a9a457.png

*我希望它能够对超过 2 个数字进行运算。

谢谢!

最佳答案

你的代码有点乱,很难理解你的意图。有关此代码的帮助将在很长一段时间内到来。我建议关注 this simple calculator tutorial .它很好地解释了概念和步骤,包括按钮事件。

关于c# - 对 C# 中的计算器操作感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9245429/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com