我创建了一个从中获取用户输入并将其与 100 个随机生成的数字进行比较的方法。一旦他们单击猜测按钮,就会显示结果。为了帮助用户,当他们将鼠标悬停在标签上时,我应该给他们一个提示,提示应该比实际数字高 3 或低 3。我无法弄清楚如何获得当用户点击猜测按钮时生成的随机数等于悬停事件的数字。对不起所有的代码,任何帮助将不胜感激。
这是我创建随机数的方式:
public partial class Form1 : Form
{
int[] rndArray = new int[100];
public void getNumbers()
{
Random random = new Random();
for (int x = 0; x < rndArray.Length; x++)
{
rndArray[x] = random.Next(1, 100);
}
}
这是 guss 按钮事件:
private void Guess_Click(object sender, EventArgs e)
{
getNumbers();
for (int x = 0; x < rndArray.Length; x++)
{
if (Convert.ToInt32(textBox1.Text) == rndArray[x])
{
result.Text = "You Win!";
correct.Text = "Correct: ";
}
else
{
result.Text = "Sorry - you loose; the number is: " + rndArray[x];
incorrect.Text = "Incorrect: ";
}
}
和鼠标悬停事件:
private void mouseHere_MouseHover(object sender, EventArgs e)
{
getNumbers();
for (int x = 0; x < rndArray.Length; x++)
hint.Text = "It's not " + (rndArray[x] +- 3);
}