【问题标题】:How to assign a value from a "for loop" to an array [duplicate]如何将“for循环”中的值分配给数组[重复]
【发布时间】:2021-05-14 22:37:56
【问题描述】:

我试图弄清楚在发生for 循环时如何将值分配给数组。我试过显示数组中的一个元素,但它总是以 0

结束
using System;

namespace Assignment4
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numArray = new int[48]; // 48 elements
            int total = 1;   // trying to get odd numbers to fill for the elements         
            int index;
            for (index = 0; index <= numArray.Length; index++) // for loop to assign array
            {
                total = total + 2;
                Console.WriteLine("#{0} current number is {1} ", index + 1, total); // double checking the index thing
            }
            Console.WriteLine(numArray[34]); // checking if the specified array will display the number in the list
        }
    }
}

【问题讨论】:

  • 您没有在 for 循环中为数组元素赋值。
  • 该值为 0,因为这是 int[] 元素的默认值,您无需更改任何元素的值。查看副本。

标签: c# arrays for-loop


【解决方案1】:

index numArray[48] = total 抛出异常,如果它是&lt;=

numArray[index] = total // 2nd change 赋值给数组

int[] numArray = new int[48]; // 48 elements
int total = 1; // trying to get odd numbers to fill for the elements         
int index;

// Changed <= to <
for (index = 0; index < numArray.Length; index++) // for loop to assign array

{
    total = total + 2;
    numArray[index] = total; // This is where you assign the values.
    Console.WriteLine("#{0} current number is {1} ", index + 1, total); // double checking the index thing
}

Console.WriteLine(numArray[34]); // checking if the specified array will display the number in the list

【讨论】:

    【解决方案2】:

    要走的路是使用赋值运算符:

    numArray[i] = y;
    

    这会将值 y 分配给数组 numArray

    的单元格 i

    【讨论】:

      【解决方案3】:

      您缺少对数组的赋值。让我们看看下面的代码。在这里,我们在 for 内部循环,以查找从 0 到 100 的奇数,如果找到,则存储在声明的数组中,索引方式。

      using System;
      
      namespace Assignment4
      {
          public class Program
          {
              public static void Main(string[] args)
              {
                  int upperLimit=100;//adding this to loop upto
                  int[] numArray = new int[48]; // 48 elements
                  int total = 1;   // trying to get odd numbers to fill for the elements         
                  int index,i=0;
                  for(index = 0; index <= upperLimit; index++) // for loop to assign array, we will go upto 100 numbers
                  {
                      total = total + 2;
                      Console.WriteLine("#{0} current number is {1} ", index+1, index+1); // double checking the index thing
                      if(index%2==1){//checking if that one is an odd number
                          Console.WriteLine("{0} is odd number",index);
                          numArray[i]=index;
                          i++;
                      }
                  }
                  Console.WriteLine(numArray[34]); // checking if the specified array will display the number in the list
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-05
        • 2016-07-06
        • 2020-02-16
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多