im在c#中的骰子模拟器中面临一个问题。函数RandomGenerator生成一对骰子,直到这两个骰子的总和等于参数中的给定数字(从2到12)。骰子对的滚动次数。问题是,当我输入一个偶数时,它正确地返回count.But当我输入一个奇数时,它什么都不做,甚至没有给出错误,短划线闪烁,闪烁。代码如下。任何人都可以帮助我?骰子模拟器int c#
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static int RandomGenerator(int n)
{
Random rand1 = new Random();
Random rand2 = new Random();
int sum = rand1.Next(1,7) + rand2.Next(1,7);
int count = 1;
{
sum = rand1.Next(1,7) + rand2.Next(1,7);
count++;
}
return count;
}
static void Main(string[] args)
{
Console.WriteLine("Hello! this program a pair of dice until total on dice is equal to your given number.\n\n");
Console.WriteLine("Enter the number :");
int num = int.Parse(Console.ReadLine());
int rolls = RandomGenerator(num);
Console.WriteLine("The number of rolls are:" + rolls);
}
}
}
你错过了一个for循环或什么?我甚至没有看到使用变量n – 2014-08-30 09:41:04
您是否尝试**调试**代码? – Carsten 2014-08-30 09:44:03
也请添加缺少的*循环*并删除第二个RNG - 只需要一个 – Carsten 2014-08-30 09:45:33