C# - 需要帮助生成随机数并在其他函数中多次使用相同的数字
C# - Need help generating a random number and using the same number multiple times in other functions
我正在尝试制作一个 psudo-Shut The Box 程序,该程序将掷骰子,用户可以选择覆盖对应于两个骰子的数字或骰子的总和。
到目前为止,除了一件事,似乎一切都运行良好。当我尝试单击任一用户输入选项的按钮时,很明显,由于选中了错误的复选框,它仍在生成随机数。
我怀疑这是我调用函数的方式造成的。有没有办法将骰子 1 和骰子 2 的随机值传递给在其他函数中调用时保持不变的值,同时在按下掷骰子按钮后能够再次随机生成?
private int firstDiceRandom()
{
Random diceOne = new Random();
int oneDie = diceOne.Next(1, 7);
System.Threading.Thread.Sleep(100);
return oneDie;
}
// Set value of Random number to a value
private int firstDiceValue()
{
int value = firstDiceRandom();
return value;
}
// Random number for Dice Two
private int secondDiceRandom()
{
Random diceTwo = new Random();
int twoDie = diceTwo.Next(1, 7);
return twoDie;
}
// Set value of Random number to a value
private int secondDiceValue()
{
int value = secondDiceRandom();
return value;
}
// Set value of Total random numbers
private int totalDice()
{
int totalDice = firstDiceValue() + secondDiceValue();
return totalDice;
}
// Check if boxes 7-12 are checked
private bool bothDiceBool()
{
if (checkBox7.Checked && checkBox8.Checked && checkBox9.Checked && checkBox10.Checked && checkBox11.Checked && checkBox12.Checked)
{
return false;
}
return true;
}
// Roll Dice Button and Generation of dice pictures for values.
private void rollDiceButton_Click(object sender, EventArgs e)
{
int oneDie = firstDiceValue();
int twoDie = secondDiceValue();
switch (oneDie)
{
case 1:
diceOneImage.Image = new Bitmap(@"location");
break;
case 2:
diceOneImage.Image = new Bitmap(@"location");
break;
case 3:
diceOneImage.Image = new Bitmap(@"location");
break;
case 4:
diceOneImage.Image = new Bitmap(@"location");
break;
case 5:
diceOneImage.Image = new Bitmap(@"location");
break;
case 6:
diceOneImage.Image = new Bitmap(@"location");
break;
}
//If 7-12 are all checked, not supposed to run, sends a blank picture where the die would be.
if (bothDiceBool() == true)
{
switch (twoDie)
{
case 1:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 2:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 3:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 4:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 5:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 6:
diceTwoImage.Image = new Bitmap(@"location");
break;
}
}
// Blank die picture
else
{
diceTwoImage.Image = new Bitmap(@"location");
}
}
// User choice button
private void button2_Click(object sender, EventArgs e)
{
int buttonOneTotal = totalDice();
// If Choice One selected when button pressed
if (radioButton1.Checked)
{
switch (buttonOneTotal)
{
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true; radioButton1.Checked = false;
break;
case 7:
checkBox7.Checked = true;
radioButton1.Checked = false;
break;
case 8:
checkBox8.Checked = true;
radioButton1.Checked = false;
break;
case 9:
checkBox9.Checked = true;
radioButton1.Checked = false;
break;
case 10:
checkBox10.Checked = true;
radioButton1.Checked = false;
break;
case 11:
checkBox11.Checked = true;
radioButton1.Checked = false;
break;
case 12:
checkBox12.Checked = true;
radioButton1.Checked = false;
break;
}
}
// If Choice two selected when button pressed
else if (radioButton2.Checked)
{
int buttonTwoOne = firstDiceValue();
int buttonTwoTwo = secondDiceValue();
// First Die
switch (buttonTwoOne)
{
case 1:
checkBox1.Checked = true;
radioButton2.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton2.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton2.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton2.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton2.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton2.Checked = false;
break;
}
// Second Die
switch (buttonTwoTwo)
{
case 1:
checkBox1.Checked = true;
radioButton1.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton1.Checked = false;
break;
}
}
}
一种可以帮助您的解决方案是在此程序范围之外声明一个变量。换句话说,一个 public 变量。
public int RandomValueOne;
现在您要做的是将您的随机值存储在此 public 变量中。
private int firstDiceValue()
{
RandomValueOne = firstDiceRandom();
return RandomValueOne;
}
现在您可以直接使用您的变量,而不必担心重新生成数字。调用你的函数 "FirstDiceValue" 一次并继续使用全局变量 isntead
我正在尝试制作一个 psudo-Shut The Box 程序,该程序将掷骰子,用户可以选择覆盖对应于两个骰子的数字或骰子的总和。
到目前为止,除了一件事,似乎一切都运行良好。当我尝试单击任一用户输入选项的按钮时,很明显,由于选中了错误的复选框,它仍在生成随机数。
我怀疑这是我调用函数的方式造成的。有没有办法将骰子 1 和骰子 2 的随机值传递给在其他函数中调用时保持不变的值,同时在按下掷骰子按钮后能够再次随机生成?
private int firstDiceRandom()
{
Random diceOne = new Random();
int oneDie = diceOne.Next(1, 7);
System.Threading.Thread.Sleep(100);
return oneDie;
}
// Set value of Random number to a value
private int firstDiceValue()
{
int value = firstDiceRandom();
return value;
}
// Random number for Dice Two
private int secondDiceRandom()
{
Random diceTwo = new Random();
int twoDie = diceTwo.Next(1, 7);
return twoDie;
}
// Set value of Random number to a value
private int secondDiceValue()
{
int value = secondDiceRandom();
return value;
}
// Set value of Total random numbers
private int totalDice()
{
int totalDice = firstDiceValue() + secondDiceValue();
return totalDice;
}
// Check if boxes 7-12 are checked
private bool bothDiceBool()
{
if (checkBox7.Checked && checkBox8.Checked && checkBox9.Checked && checkBox10.Checked && checkBox11.Checked && checkBox12.Checked)
{
return false;
}
return true;
}
// Roll Dice Button and Generation of dice pictures for values.
private void rollDiceButton_Click(object sender, EventArgs e)
{
int oneDie = firstDiceValue();
int twoDie = secondDiceValue();
switch (oneDie)
{
case 1:
diceOneImage.Image = new Bitmap(@"location");
break;
case 2:
diceOneImage.Image = new Bitmap(@"location");
break;
case 3:
diceOneImage.Image = new Bitmap(@"location");
break;
case 4:
diceOneImage.Image = new Bitmap(@"location");
break;
case 5:
diceOneImage.Image = new Bitmap(@"location");
break;
case 6:
diceOneImage.Image = new Bitmap(@"location");
break;
}
//If 7-12 are all checked, not supposed to run, sends a blank picture where the die would be.
if (bothDiceBool() == true)
{
switch (twoDie)
{
case 1:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 2:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 3:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 4:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 5:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 6:
diceTwoImage.Image = new Bitmap(@"location");
break;
}
}
// Blank die picture
else
{
diceTwoImage.Image = new Bitmap(@"location");
}
}
// User choice button
private void button2_Click(object sender, EventArgs e)
{
int buttonOneTotal = totalDice();
// If Choice One selected when button pressed
if (radioButton1.Checked)
{
switch (buttonOneTotal)
{
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true; radioButton1.Checked = false;
break;
case 7:
checkBox7.Checked = true;
radioButton1.Checked = false;
break;
case 8:
checkBox8.Checked = true;
radioButton1.Checked = false;
break;
case 9:
checkBox9.Checked = true;
radioButton1.Checked = false;
break;
case 10:
checkBox10.Checked = true;
radioButton1.Checked = false;
break;
case 11:
checkBox11.Checked = true;
radioButton1.Checked = false;
break;
case 12:
checkBox12.Checked = true;
radioButton1.Checked = false;
break;
}
}
// If Choice two selected when button pressed
else if (radioButton2.Checked)
{
int buttonTwoOne = firstDiceValue();
int buttonTwoTwo = secondDiceValue();
// First Die
switch (buttonTwoOne)
{
case 1:
checkBox1.Checked = true;
radioButton2.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton2.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton2.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton2.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton2.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton2.Checked = false;
break;
}
// Second Die
switch (buttonTwoTwo)
{
case 1:
checkBox1.Checked = true;
radioButton1.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton1.Checked = false;
break;
}
}
}
一种可以帮助您的解决方案是在此程序范围之外声明一个变量。换句话说,一个 public 变量。
public int RandomValueOne;
现在您要做的是将您的随机值存储在此 public 变量中。
private int firstDiceValue()
{
RandomValueOne = firstDiceRandom();
return RandomValueOne;
}
现在您可以直接使用您的变量,而不必担心重新生成数字。调用你的函数 "FirstDiceValue" 一次并继续使用全局变量 isntead