【发布时间】:2016-02-09 12:08:56
【问题描述】:
好的,我正在尝试制作一个 GUI 骰子游戏,其中骰子数字显示在文本框中。
我必须创建一个代表骰子的类,并且我的至少一个方法必须通过引用正确传递参数。我的问题是我对类或传递参数不太熟悉
我收到一个错误 rd.RollDice1(参考骰子1); rd.RollDice2(参考骰子2); (我确定我没有错误地构造 RollDice 类) 请问有人可以帮我吗?
到目前为止,这是我的代码:
public partial class Form1 : Form
{
private RollDice rd;
public Form1()
{
InitializeComponent();
rd = new RollDice();
}
private void button1_Click(object sender, EventArgs e)
{
int dice1, dice2;
const int EYE = 1;
const int BOX = 6;
rd.RollDice1(ref dice1);
rd.RollDice2(ref dice2);
string result = string.Format("{0}", dice1);
string result2 = string.Format("(0)", dice2);
textBox1.Text = result;
textBox2.Text = result;
if (dice1 == EYE && dice2 == BOX)
{
MessageBox.Show("You rolled a Snake Eyes!");
}
if (dice1 == BOX && dice2 == BOX)
{
MessageBox.Show("You rolled BoxCars!");
}
else
{
MessageBox.Show("You rolled a {0} and a {1}", dice1, dice2);
}
}
}
}
class RollDice
{
const int EYE = 1;
const int BOX = 6;
public int RollDice1(ref int dieValue1)
{
Random randomNums = new Random();
dieValue1 = randomNums.Next(1, 7);
return dieValue1;
}
public int RollDice2(ref int dieValue2)
{
Random randomNums = new Random();
dieValue2 = randomNums.Next(1, 7);
return dieValue2;
}
}
}
【问题讨论】:
-
"I get an error at..."- 错误是什么?另外,您为什么要通过引用传递 并 返回值?不妨选择两种方法中的一种。 -
啊这就是为什么...我会得到回报并继续努力。谢谢!
标签: c# user-interface dice