到目前为止,我已经得到随机数和单选按钮,我的具体问题是如果用户猜错了如何显示实际答案。我会在if / else语句的labelOutput上显示这个。此外,我还想提一下人们猜测在labelHint上说偶数或奇数之前的实际数字。您可以通过代码中的注释查看我正在处理的部分。
namespace GuessANumber
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
labelHint.Text = "Hover over for a hint";
labelOutput.Visible = true;
Init();
}
Random random = new Random();
int correctAnswer;
private void Init()
{
correctAnswer = random.Next(1, 6);
}
private bool UserSelectedRandomButton()
{
if (radioButton1.Checked == true)
return (correctAnswer == 1);
if (radioButton2.Checked == true)
return (correctAnswer == 2);
if (radioButton3.Checked == true)
return (correctAnswer == 3);
if (radioButton4.Checked == true)
return (correctAnswer == 4);
if (radioButton5.Checked == true)
return (correctAnswer == 5);
return false;
}
private void buttonSubmit_Click(object sender, EventArgs e)
{
radioButton1.Enabled = false;
radioButton2.Enabled = false;
radioButton3.Enabled = false;
radioButton4.Enabled = false;
radioButton5.Enabled = false;
labelOutput.Visible = true;
if (UserSelectedRandomButton() == true)
{
labelOutput.Text = "You are correct!";
}
else
{
labelOutput.Text = (" Sorry, the correct answer is" + " " + correctAnswer);//now working :)
}
correctAnswer = random.Next(1, 6);
}
private void labelHint_MouseEnter(object sender, EventArgs e)
{
//still working on this, if the number is odd, label changes to odd statement, if even says even statement
labelHint.Text = "The number is odd";
}
private void labelHint_MouseLeave(object sender, EventArgs e)
{
labelHint.Text = "Hover over for a hint";
}
private void buttonReset_Click(object sender, EventArgs e)
{
radioButton1.Enabled = true;
radioButton2.Enabled = true;
radioButton3.Enabled = true;
radioButton4.Enabled = true;
radioButton5.Enabled = true;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
labelOutput.Visible = false;
}
}
}
答案 0 :(得分:1)
两个提示都不起作用,而且没有显示"正确"答案有同样的根本问题。您正在UserSelectedRandomButton()
函数中抓取一个新的随机数,然后有效地将其丢弃(因为它超出了范围)。
获得正确答案"功能正常,您需要将该数字分配给类级别变量,然后引用它而不是再次调用UserSelectedRandomButton
,如下所示:
//This is our class level variable
int correctAnswer;
private bool UserSelectedRandomButton()
{
Random random = new Random();
correctAnswer = random.Next(1, 6);
if (radioButton1.Checked == true)
return (correctAnswer == 1);
if (radioButton2.Checked == true)
return (correctAnswer == 2);
if (radioButton3.Checked == true)
return (correctAnswer == 3);
if (radioButton4.Checked == true)
return (correctAnswer == 4);
if (radioButton5.Checked == true)
return (correctAnswer == 5);
return false;
}
private void buttonsubmit_Click(object sender, EventArgs e)
{
radioButton1.Enabled = false;
radioButton2.Enabled = false;
radioButton3.Enabled = false;
radioButton4.Enabled = false;
radioButton5.Enabled = false;
if (UserSelectedRandomButton() == true)
{
labelOutput.Text = "You are correct!";
}
else
{
labelOutput.Text = (" Sorry, the correct answer is" + " " + correctAnswer);//now working :)
}
}
现在要获得提示,你必须做同样的事情(分配给类级变量),但是有一个问题。当用户按下"提交"时,将绘制随机数。按钮,所以当我们显示提示时,我们不知道数字是多少!可能最简单的解决方法是在程序启动时绘制一个数字(我将它放在" Init"可以从某个地方调用的函数)然后在完成后更新它签入"提交" (它也可以在"重置")中完成。
Random random = new Random();
//This is our class level variable
int correctAnswer;
private void Init()
{
correctAnswer = random.Next(1, 6);
}
private bool UserSelectedRandomButton()
{
if (radioButton1.Checked == true)
return (correctAnswer == 1);
if (radioButton2.Checked == true)
return (correctAnswer == 2);
if (radioButton3.Checked == true)
return (correctAnswer == 3);
if (radioButton4.Checked == true)
return (correctAnswer == 4);
if (radioButton5.Checked == true)
return (correctAnswer == 5);
return false;
}
private void buttonsubmit_Click(object sender, EventArgs e)
{
radioButton1.Enabled = false;
radioButton2.Enabled = false;
radioButton3.Enabled = false;
radioButton4.Enabled = false;
radioButton5.Enabled = false;
if (UserSelectedRandomButton() == true)
{
labelOutput.Text = "You are correct!";
}
else
{
labelOutput.Text = (" Sorry, the correct answer is" + " " + correctAnswer);//now working :)
}
correctAnswer = random.Next(1, 6);
}
您的工具提示会检查" correctAnswer"变量在决定说"偶然"或"奇数"。您还应该注意到我将Random
类实例移动到类范围中(而不是将其放在函数范围内)。这被认为是Random类的最佳实践,因为每次使用它时重新创建它都会减少随机性"随机性。生成的数字。它还有助于代码重用,因为我们现在在两个地方绘制一个数字。
让我知道我是否可以澄清任何事情!