【问题标题】:How to measure the amount of times 0 & 1 occurs如何测量 0 和 1 出现的次数
【发布时间】:2017-03-06 08:44:05
【问题描述】:

这是根据 2 个数组的相等性输出 1 或 0 的代码

static void Main(string[] args)
        {

            while (true)
            {
                Console.WriteLine(Here());  
            }

这就是魔法发生的地方。

static int Here()
        {
            Random rnd = new Random();
            PlayerInput();
            int[] intarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            string[] sarray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };


                int i = rnd.Next(10); // creates a number between 1 and 10
                int x = rnd.Next(10);
                string iarray = intarray[x].ToString();
                if (iarray == sarray[i])
                {
                    return 1;
                }
                else
                {
                    return 0;
                }



        }

当两个数组不相等时输出 0,反之则输出 1。现在我想计算它输出 0 和 1 的次数。

问题:

  1. 我该怎么做?

  2. 是否应该将输出传输到数组以便于操作?

【问题讨论】:

  • 你什么时候打破你的while循环?
  • 你熟悉 lamda 表达式吗?您可以使用以下.GroupBy, .Where, and .Select 轻松做到这一点,如果您需要查看工作示例,我可以在 List 中发布使用非重复项和重复项的示例
  • 您的while 循环应该如何退出?永远不要使用while(true),除非你真的,真的知道你在做什么。这将无限循环。
  • 另外,你为什么要将ints 转换为Strings 来比较它们?只需将它们与int 值进行比较

标签: c# boolean logic console-application


【解决方案1】:

除非我误解了您的问题,否则您似乎可以只使用两个计数器变量:

static void Main(string[] args)
{
    int totalOnes = 0;
    int totalZeroes = 0;

    while (true) // need to replace this with something that will actually exit!!
    {
        int ret = Here();
        if (ret == 1) totalOnes++; else totalZeroes++;
        Console.WriteLine(ret);  
    }

    Console.WriteLine("Total Ones: {0} Total Zeroes: {1}", totalOnes, totalZeroes);
}

编辑:感谢 L J 指出您的 while 循环将永远不会退出,因为您有 while (true)。你需要解决这个问题。

【讨论】:

  • 哦,你是第一个……好吧。但请记住,这里if (ret == 1) totalOnes++; else totalZeroes++;totalZeroes 不是零计数器,而是它的“非一”计数器,因为这里将来可以返回 2 或 10。
  • 可以吗?正如他给的,回报只能是01
  • @tym32167 -- 我同意汉克的观点。这个问题清楚地表明它返回零或一。你的方法很古怪;如果我想考虑其他可能的情况,我只会使用switch
  • @rory.ap ofcource 两个代码都可以工作,我不想说你的代码不好,只是根据经验,需求经常变化,变量名称应该反映变量值。在您的情况下,totalZeroes 表示结果不等于 1 的次数,我只是想注意它。
  • 只是一个输入:控件不可能移出while循环
【解决方案2】:

喜欢这个

static void Main(string[] args)
{
    var zeroCount = 0;
    var oneCount = 0;
    while (true)
    {
        var result = Here();
        if (result == 1) oneCount++;
        if (result == 0) zeroCount++;
        Console.WriteLine($"Actual result {result}, zero count {zeroCount}, One count {oneCount}");     
    }
}

【讨论】:

  • int Here() 的@Tinwor 结果可以是任何东西,因为它返回int。将此功能视为黑匣子。
  • @tym32167 从技术上讲,OP 可以编辑他的Here() 方法以返回任何int,是的。但是,我们被要求为他当前的代码提供答案,而不是他的代码在未来的可能性。 Here() 确实返回了 int,但返回是在当前状态下对 ints 的有限选择。如果 OP 从不需要该方法返回 0 或 1 以外的任何值,那么为什么还要考虑其他任何内容?
  • 我不想说这段代码是完美的,我只是提到变量名称应该适合变量值。在我的情况下,zeroCount 表示有多少结果为零。如果是来自 rory.ap 的回答,则表示有多少结果不等于 1
  • 如果您使用 if 语句并在没有其他因素的情况下单独查看它,那将是正确的。但是,考虑到当前的Here() 方法,他的totalZeros 确实意味着0 有多少结果。它在情境上是正确的,但普遍含糊不清​​。我明白你的意思,但在对变量名称的歧义形成意见时,你必须考虑变量的情况。
  • 哦,没关系,别理我,应该少工作多休息... :)
【解决方案3】:
static int Here()
        {

            Random rnd = new Random();
            PlayerInput();
            int[] intarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            string[] sarray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

                **var Count1 = 0;
                var Count0 = 0;**
                int i = rnd.Next(10); // creates a number between 1 and 10
                int x = rnd.Next(10);
                string iarray = intarray[x].ToString();
                if (iarray == sarray[i])
                {
                    **Count1++;**
                    return 1;
                }
                else
                {
                    **Count0++;**
                    return 0;
                }



        }

Count0 将包含输出 0 的次数。

Count1 将包含输出 1 的次数。

【讨论】:

  • Count1Count0 目前在 Here() 之外是不可访问的变量,因为该方法是私有的。
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 2012-08-07
  • 2015-11-22
  • 2015-06-06
  • 1970-01-01
相关资源
最近更新 更多