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