【发布时间】: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[]元素的默认值,您无需更改任何元素的值。查看副本。