C#骰子游戏逻辑错误

时间:2017-10-13 22:37:08

标签: c# arrays

我正在学习C#,我正在编写一个程序,使一个数组用50个随机数字填充它,像骰子一样,然后检查每个值或“边”出现的次数。本周早些时候我在python中制作了一个骰子游戏,我遇到了很多麻烦,所以我做了i = j =和match = print out来测试循环和匹配是否正确迭代,所以我在这里做了同样的事情,我注意到了一些逻辑错误:

  1. i循环似乎迭代很好但是对于i的每1次迭代,j应该迭代50次但我只得到它一次。

  2. i OR j循环根本不迭代,除非在第47行中它表示j> dice.Length。写作应该是j< dice.Length使它完全没有迭代。屏幕上显示50个随机数,所以我知道骰子长度为50,j为0。

  3. 第三行在第50行,如果骰子[i] ==骰子[j]我得到一个错误,j无效,除非我在for循环上面声​​明j,如果我这样做,我就做不到for循环中的int j = 0,所以我取消了for循环并执行了一个while循环,但它仍然只为第一个匹配添加了值而不是下一个可能的49.

  4. 我只是编写静态void,因为它是一个简单的控制台应用程序,谢谢你的帮助。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace wk8hw2
    {
        class Program
        {
            static void Main(string[] args)
            {//staticvoid
                int size = 50;
                int diceSides = 7;
                int matchAdd = 0;
                int[] dice = new int[size];
                int[] match = new int[diceSides];
                Random rnd = new Random();
                int j = 0;
    
                Console.WriteLine("Press any key to roll the dice " + size + " times.");
                Console.ReadKey();
    
                for (int i = 0; i < dice.Length; i++)//roll dice
                {
                    dice[i] = rnd.Next(1, diceSides);
                }
                for (int i = 0; i < dice.Length; i++)//show dice
                {
                    Console.Write(dice[i] + " ");
                }
                Console.WriteLine("done showing dice");//DEBUG
                for (int i = 0; i < dice.Length; i++)//outer match loop
                {
                    Console.Write("i = " + i);//DEBUG
                    if (match[dice[i]] == 0)//if not matched add to match array
                    {
                        Console.WriteLine("not yet matched");
                        matchAdd = 1;
                    }
                    else//if alerady matched add no more
                    {
                        Console.WriteLine("already matched");
                        matchAdd = 0;
                    }
                    j = 0;
                    while (j > dice.Length)//inner match loopSUPPOSED TRO BE LESS THAN
                    {
                        Console.WriteLine("j = " + j);
                        if (dice[i] == dice[j])//if equal add to match array
                        {
                            match[dice[i]] = match[dice[i]] + matchAdd;
                            Console.WriteLine("val " + match[dice[i]]);
                        }
                        j++;
                    }
                }//endFORouter
    
                for (int i = 1; i < match.Length; i++)
                {
                    Console.WriteLine(i + " came up " + match[i] + " times.");
                }
    
                Console.ReadKey();
            }//endstaticvoid
        }
    }
    

2 个答案:

答案 0 :(得分:0)

我将这整件重写为:

Random rnd = new Random();
const int diceSides = 6;
const int numDice = 50;

Console.WriteLine("Press any key to roll the dice {0} times.", numDice);
Console.ReadKey();

var diceByValue = Enumerable.Range(0, numDice)
                      .Select(_ => rnd.Next(diceSides) + 1)
                      .GroupBy(v => v)
                      .OrderBy(g => g.Key);

foreach (var group in diceByValue)
    Console.WriteLine("{0} came up {1} times.", group.Key, group.Count());

答案 1 :(得分:0)

我同意第一个答案但是......你说你正在学习C#并且基于命名空间这是一个家庭作业,你很可能会学习数组,所以我会重写它有点不同

你似乎正在为许多循环做准备并且通常做很多事情。让代码尽可能简单,并命名变量,告诉任何人阅读代码的内容。

int numberOfRolls = 50;
int diceSides = 6;
int[] dice = new int[numberOfRolls];
int[] match = new int[diceSides];
Random random = new Random();

Console.WriteLine("Press any key to roll the dice " + numberOfRolls + " times.");
Console.ReadKey();

for (int rollCount = 0; rollCount < numberOfRolls; rollCount++)
{
    var rollResult = random.Next(1, diceSides+1);
    match[rollResult-1]++;
}

for (int i = 0; i < match.Length; i++)
{
    Console.WriteLine(i+1 + " came up " + match[i] + " times.");
}

Console.ReadKey();