【问题标题】:breaks while loop when all die rolls the same number当所有骰子掷出相同的数字时,会中断 while 循环
【发布时间】:2021-11-26 21:09:09
【问题描述】:
using System;
using System.Collections.Generic;
namespace AnyDice
{
    class Program
    {
        static void Main(string[] args)
        {
            int diceSides;
            int rollDie;
            int count = 0;
            bool keepRolling = true;
            List<int> num = new List<int>();
            Random random = new Random();

            Console.Write("Write the number of sides of your die: ");
            diceSides = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Type the numbers of the die");

            for (int i = 0; i < diceSides; i++)
            {
                int rank = 1 + i;
                Console.Write(rank + "~~> ");
                num.Add(Convert.ToInt32(Console.ReadLine()));
            }

            num.Sort();

            Console.WriteLine("\nHere's the die and its contents");
            for (int i = 0; i < num.Count; i++)
            {
                Console.Write("[");
                Console.Write(num[i]);
                Console.Write("]");
            }

            Console.WriteLine("\nHow many times do you want to roll at once");
            rollDie = Convert.ToInt32(Console.ReadLine());
            while (keepRolling)
            {
                for (int i = 0; i < rollDie; i++)
                {
                    Console.Write("[");
                    Console.Write(num[random.Next(num.Count)]);
                    Console.Write("]");
                    count++;
                }
                Console.ReadLine();
            }

            Console.WriteLine("It took you " + count + " attempts");

            Console.ReadLine();
        }
    }
}

例如,如果 (4,4,4,4) 被滚动或 (2,2) 在任何“n”个列中,while 循环就会中断。 我想将每个骰子滚动值存储在另一个数组列表中并比较其中的每个值。如果它都相等,那么它会中断..但我不知道如何实现它。

【问题讨论】:

  • if (num.Distinct().Count() == 1) { }
  • 我编辑了您的代码示例,因此每行有一个语句。在 C# 中将多个语句放在同一行是极为罕见的,我认为这会使任何试图回答您的问题的人感到困惑。
  • 当所有掷出的骰子都是相同的确切数字时,基本会中断while循环
  • @jdweng num 是模具,而不是卷。
  • 添加List&lt;int&gt; 以跟踪卷:var rolls = new List&lt;int&gt;();。将每个卷存储在列表中:var roll = num[random.Next(num.Count)]; rolls.Add(roll);Console.Write(roll); 然后测试卷是否相同:keepRolling = rolls.Distinct().Count() == 1;

标签: c# random die


【解决方案1】:

我们有 Linq。它位于 System.Linq 命名空间中,这可能会对您有所帮助。

我应该用两种方法来检查是否所有的死都是一样的:

int first = dies.First();
if (dies.All(i => i == first))
{
  // break if all are equals to the first die
}

或者使用Distinct,我们可以过滤掉任何副本。

if (dies.Distinct().Count() == 1)
{
  // if we only have unique items and the count is 1 every die is the same
}

【讨论】:

  • 这是一个可怕的解决方案。当您只能使用一个变量时,您正在使用两个变量。
【解决方案2】:
            while (keepRolling)
            {
                rolls.Clear();
                for (int i = 0; i < rollDie; i++)
                {
                    var firstRoll = num[random.Next(num.Count)];
                    rolls.Add(firstRoll);
                    Console.Write(firstRoll + " ");
                    count++;
                }
                if (rolls.Distinct().Count() == 1)
                {
                    Console.WriteLine("It took you " + count + " attempts");
                    keepRolling = false;
                    break;
                }
                Console.ReadLine();
            }

【讨论】:

  • 欢迎来到 Stack Overflow,感谢您提供答案。您能否编辑您的答案以包括对您的代码的解释?这将有助于未来的读者更好地了解正在发生的事情,尤其是那些刚接触该语言并难以理解概念的社区成员。
【解决方案3】:

我不是 100% 确定我理解您的要求,但无论如何,您应该编写一个单独的函数,该函数返回一个标志,指示数组是否处于应该触发中断的状态。

bool KeepRolling(int[] num)
{
    for (int i=0; i<num.Length; i++)
    {
        if (num[i] >= i) return false;
    }
    return true;
}

然后在你的循环中调用它:

keepRolling = KeepRolling(num);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 2014-02-28
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 2012-11-27
    • 2018-02-27
    相关资源
    最近更新 更多