【发布时间】:2012-11-27 11:03:31
【问题描述】:
C# 新手,很抱歉,如果这很愚蠢。
到目前为止,关于我的骰子,我有以下代码:
Random DiceRandom = new Random();
int DiceThrow = DiceRandom.Next(1, 7);
Console.WriteLine(DiceThrow);
Console.ReadLine();
Console.WriteLine("Player 1 rolled a " + DiceThrow);
Console.ReadLine();
Console.WriteLine("Player 2 rolled a " + DiceThrow);
Console.ReadLine();
Console.WriteLine("Player 3 rolled a " + DiceThrow);
Console.ReadLine();
Console.WriteLine("Player 4 rolled a " + DiceThrow);
Console.ReadLine();
现在,这会创建一个很好的数字并显示它,但是每个玩家的数字都是相同的。
我需要一个循环,为每个玩家重复掷骰,如果有玩家掷出 6,我将再次掷骰。
【问题讨论】:
-
来一个!当然你可以计算出你需要调用 DiceThrow = DiceRandom.Next(1, 7);每次...如果您设置一次变量(以任何语言),您希望它具有什么值?
-
@MitchWheat 这对于刚接触这个的人来说并不一定很明显。如果您不是很熟悉 C# 和一般编程,
int DiceThrow = DiceRandom.Next(1, 7);行可以看作是函数的内联定义。 -
@Jeff:初学者比函数委托更可能假设简单的变量赋值。对吗?
-
@MitchWheat 可能更有可能,但是比编码更习惯数学的人可以合理地将声明视为陈述等价而不是赋值。跨度>
-
我只能为自己说话:我拥有纯数学学位。我记得学习编程。首先想到的肯定是变量赋值。