一定要遗漏一些基本的东西,但是试着制作一个简单的计算器应用程序,这是我的代码的一部分,当我试图运行它给它的时候;
"错误1无法使用本地变量' xxxxxx'在宣布之前。局部变量的声明隐藏字段xxxxx
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double total1 = 0;
double total2 = 0;
bool plusBtnPressed = false;
bool minusBtnPressed = false;
bool timesBtnPressed = false;
bool divideBtnPressed = false;
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
if(plusBtnPressed == true) {
total2 = total1 + double.Parse(txtDisplay.Text);
}
else if (minusBtnPressed == true) {
total2 = total1 - double.Parse(txtDisplay.Text);
}
else if (divideBtnPressed == true) {
total2 = total1 / double.Parse(txtDisplay.Text);
}
else if (timesBtnPressed == true) {
total2 = total1 * double.Parse(txtDisplay.Text);
}
bool plusBtnPressed = false;
bool minusBtnPressed = false;
bool timesBtnPressed = false;
bool divideBtnPressed = false;
}
private void btnPlus_Click(object sender, EventArgs e)
{
total1 += double.Parse(txtDisplay.Text);
plusBtnPressed = true;
minusBtnPressed = false;
divideBtnPressed = false;
timesBtnPressed = false;
txtDisplay.Clear();
}
答案 0 :(得分:4)
从bool
事件处理程序中删除bool plusBtnPressed = false;
button3_Click
,并为其他3个变量执行此操作。
您目前正在声明新变量,这会隐藏类成员。编译器还认为您正在使用导致错误的函数中较早的那些声明变量。
答案 1 :(得分:1)
相反,
bool plusBtnPressed = false;
bool minusBtnPressed = false;
bool timesBtnPressed = false;
bool divideBtnPressed = false;
穿戴,
plusBtnPressed = false;
minusBtnPressed = false;
timesBtnPressed = false;
divideBtnPressed = false;
在你的事件处理程序中。