【问题标题】:Method 2D-Array display numbers方法 2D-Array 显示数字
【发布时间】:2014-05-25 03:24:05
【问题描述】:

您好,我有一个问题。我的代码应该显示随机数并将所有奇数相加,但我的问题是它只生成一堆 0。如果有人愿意帮助我,我将非常感谢。

我写了很多类似的代码,但这是唯一一个只生成 0 的代码,我不知道它有什么问题。

    int[,] A = new int[5, 7];
    Random rand = new Random();

    private void SumOdd(int[,] array)
    {
        int sum = 0;
        int rows = array.GetLength(0);
        int cols = array.GetLength(1);

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                if (sum % 2 != 0)
                {
                    array[i, j] = rand.Next(-100, 100);
                    sum += array[i, j];
                }
                richTextBox1.AppendText(array[i, j] + " ");
            }
        }
        richTextBox1.AppendText("The Sum of all Odd is: "+ sum.ToString());
    }

【问题讨论】:

  • 你试过调试吗?在你的if 语句中设置一个断点,你就会知道哪里出了问题。
  • @DatVM 它告诉我 array[i,j] = rand.Next(-100, 100);它有问题,但我应该把它放在哪里?

标签: c# arrays methods multidimensional-array


【解决方案1】:

我看到一个“奇怪”的事情:首先你用0 初始化sum。到目前为止,一切都很好。然后你测试它是否与sum % 2 != 0 奇怪。由于sum 为零,因此该标准永远不会为真,因此它永远不会进入将改变总和的范围内。

我想你想测试新随机数的奇数,所以试试这个:

int[,] A = new int[5, 7];
Random rand = new Random();

private void SumOdd(int[,] array)
{
    int sum = 0;
    int rows = array.GetLength(0);
    int cols = array.GetLength(1);

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            int value = rand.Next(-100, 100); // Create a new random number.
            array[i, j] = value;  // Store it in the array.
            richTextBox1.AppendText(value + " "); // Display it in the textbox.
            if (value % 2 != 0) // Test if it is odd.
                sum += value; // If so... add it to the sum.
        }
    }
    richTextBox1.AppendText("The Sum of all Odd is: "+ sum.ToString());
}

【讨论】:

    【解决方案2】:
    sum = 0;
    
    if (sum % 2 != 0) 
    {
      // never reaches here
      // sum % 2 is always 0 in your case
    }
    

    【讨论】:

      【解决方案3】:
      if (sum % 2 != 0)
      

      当代码到达这一点时,sum 最初为 0。因此,在计算时,0 / 2 保持为 0。if 中的块只有在余数不为 0 时才会执行。

      array[i, j] = rand.Next(-100, 100);
      

      在 if 之外,如果您希望在 sum 更改值之前随机化数字。

      另外,你说你想添加奇数;好吧,这会检查总和,而不是随机生成的数字。只是一个想法。

      【讨论】:

        【解决方案4】:

        我使用控制台应用程序构建它,效果很好

         static Random rand; 
                    static void Main(string[] args)
                    {
                        int[,] A = new int[5, 7];
                      rand = new Random();
        
                      SumOdd(ref A);                 
        
        
                        Console.ReadLine();
                    }
        
                    private static void SumOdd(ref int[,] array)
                    {
                        int sum = 0;
                        int rows = array.GetLength(0);
                        int cols = array.GetLength(1);
        
                        for (int i = 0; i < rows; i++)
                        {
                            for (int j = 0; j < cols; j++)
                            {
                                array[i, j] = rand.Next(-100, 100);
                                if (array[i,j] % 2 != 0)
                                {
                                    Console.WriteLine(array[i, j]);
                                    sum += array[i, j];
                                }
                                Console.WriteLine(array[i, j] + " ");
                            }
                        }
                       Console.WriteLine("The Sum of all Odd is: " + sum.ToString());
                    }
        

        【讨论】:

          猜你喜欢
          • 2014-05-25
          • 2013-04-21
          • 2016-04-25
          • 2014-06-15
          • 2011-08-12
          • 2017-03-14
          • 2020-05-07
          • 2019-03-12
          • 1970-01-01
          相关资源
          最近更新 更多