如何将单选按钮选择与 C# 中的变量值进行比较?
How can I compare a radio button selection to a variable value in C#?
这是我的 C# class 教科书中的作业,我永远无法正常工作。我在网上搜索,找不到答案。我问了我的教授和其他学生,他们要么给了我非答案(一次调试一个问题),要么给了我没有的问题的答案。我觉得这一定是我们在这个基本介绍 class 中学到的超级简单的东西,但我想不通。具体提示供参考:
Create a project named GuessANumber with a Form that contains a guessing
game with five RadioButtons numbered 1 through 5. Randomly choose one of the
RadioButtons as the winning button. When the user clicks a RadioButton, display a
message indicating whether the user is right.
Add a Label to the Form that provides a hint. When the user’s mouse hovers over the
label, notify the user of one RadioButton that is incorrect. After the user makes a
selection, disable all the RadioButtons.
我能够生成 运行dom 号码,创建一个标签,当鼠标悬停在上面时会显示一个错误答案,一旦用户点击一个单选按钮,它就会禁用其他按钮。
我 运行 遇到的问题是试图将单击的单选按钮与 运行dom 号码答案进行比较。
首先,我尝试像这样比较事件处理程序中的值:
private void AnswerButton1_CheckedChanged(object sender, EventArgs e)
{
AnswerButton2.Enabled = false;
AnswerButton3.Enabled = false;
AnswerButton4.Enabled = false;
AnswerButton5.Enabled = false;
if (randomNumber == 1)
{
ResultLabel.Text = "Correct";
}
else
{
ResultLabel.Text = "Incorrect";
}
}
我意识到我不知道如何将 运行domNumber 的值带入事件处理程序方法。因此,我尝试设置一个 if/else 循环来检查使用单选按钮选择了哪个按钮。选中 属性。我在 Form1 中执行此操作,其中 运行domNumber 已初始化并分配,因此我可以相互检查它们,并且我还尝试为 运行 这个循环创建一个单独的方法,我传递了 运行domNumber 到。像这样:
if (AnswerButton1.Checked && answer == 1)
{
ResultLabel.Text = "Correct";
}
这两种方法都有效,但它们都有相同的问题,即在加载表单时立即循环 运行,因此它总是发现 else 值(未选择按钮)为真。在那个时候点击一个按钮没有任何效果。
所以我的问题是,如何根据程序初始化时分配的值检查单选按钮选择?
"I didn't know how to bring the value of randomNumber into the event handler methods"
这听起来像是在方法内部声明随机数,这意味着它只具有该方法的作用域。相反,创建一个 class 字段来存储值,因此 class:
中的所有方法都可以访问它
public partial class Form1 : Form
{
// Give our random number class-level scope, so all class methods can access it
private int randomNumber;
private Random random = new Random();
public Form1()
{
InitializeComponent();
// Set the random number when the form starts
this.randomNumber = random.Next(1, 6);
}
private void AnswerButton1_CheckedChanged(object sender, EventArgs e)
{
AnswerButton2.Enabled = false;
AnswerButton3.Enabled = false;
AnswerButton4.Enabled = false;
AnswerButton5.Enabled = false;
// Now we have access to the random number because it has class-level scope
if (this.randomNumber == 1)
{
ResultLabel.Text = "Correct";
}
else
{
ResultLabel.Text = "Incorrect";
}
}
}
这是我的 C# class 教科书中的作业,我永远无法正常工作。我在网上搜索,找不到答案。我问了我的教授和其他学生,他们要么给了我非答案(一次调试一个问题),要么给了我没有的问题的答案。我觉得这一定是我们在这个基本介绍 class 中学到的超级简单的东西,但我想不通。具体提示供参考:
Create a project named GuessANumber with a Form that contains a guessing game with five RadioButtons numbered 1 through 5. Randomly choose one of the RadioButtons as the winning button. When the user clicks a RadioButton, display a message indicating whether the user is right.
Add a Label to the Form that provides a hint. When the user’s mouse hovers over the label, notify the user of one RadioButton that is incorrect. After the user makes a selection, disable all the RadioButtons.
我能够生成 运行dom 号码,创建一个标签,当鼠标悬停在上面时会显示一个错误答案,一旦用户点击一个单选按钮,它就会禁用其他按钮。
我 运行 遇到的问题是试图将单击的单选按钮与 运行dom 号码答案进行比较。
首先,我尝试像这样比较事件处理程序中的值:
private void AnswerButton1_CheckedChanged(object sender, EventArgs e)
{
AnswerButton2.Enabled = false;
AnswerButton3.Enabled = false;
AnswerButton4.Enabled = false;
AnswerButton5.Enabled = false;
if (randomNumber == 1)
{
ResultLabel.Text = "Correct";
}
else
{
ResultLabel.Text = "Incorrect";
}
}
我意识到我不知道如何将 运行domNumber 的值带入事件处理程序方法。因此,我尝试设置一个 if/else 循环来检查使用单选按钮选择了哪个按钮。选中 属性。我在 Form1 中执行此操作,其中 运行domNumber 已初始化并分配,因此我可以相互检查它们,并且我还尝试为 运行 这个循环创建一个单独的方法,我传递了 运行domNumber 到。像这样:
if (AnswerButton1.Checked && answer == 1)
{
ResultLabel.Text = "Correct";
}
这两种方法都有效,但它们都有相同的问题,即在加载表单时立即循环 运行,因此它总是发现 else 值(未选择按钮)为真。在那个时候点击一个按钮没有任何效果。
所以我的问题是,如何根据程序初始化时分配的值检查单选按钮选择?
"I didn't know how to bring the value of randomNumber into the event handler methods"
这听起来像是在方法内部声明随机数,这意味着它只具有该方法的作用域。相反,创建一个 class 字段来存储值,因此 class:
中的所有方法都可以访问它public partial class Form1 : Form
{
// Give our random number class-level scope, so all class methods can access it
private int randomNumber;
private Random random = new Random();
public Form1()
{
InitializeComponent();
// Set the random number when the form starts
this.randomNumber = random.Next(1, 6);
}
private void AnswerButton1_CheckedChanged(object sender, EventArgs e)
{
AnswerButton2.Enabled = false;
AnswerButton3.Enabled = false;
AnswerButton4.Enabled = false;
AnswerButton5.Enabled = false;
// Now we have access to the random number because it has class-level scope
if (this.randomNumber == 1)
{
ResultLabel.Text = "Correct";
}
else
{
ResultLabel.Text = "Incorrect";
}
}
}