C# 如何从“a”中赋值;for loop";排列

C# 如何从“a”中赋值;for loop";排列,c#,arrays,for-loop,C#,Arrays,For Loop,我正在试图弄清楚如何在发生for循环时为数组赋值。我尝试显示数组中的一个元素,但结果总是0 使用系统; 名称空间分配4 { 班级计划 { 静态void Main(字符串[]参数) { int[]numaray=新int[48];//48个元素 int total=1;//尝试获取奇数以填充元素 整数指数; 对于(index=0;indexindex

我正在试图弄清楚如何在发生
for
循环时为数组赋值。我尝试显示数组中的一个元素,但结果总是0

使用系统;
名称空间分配4
{
班级计划
{
静态void Main(字符串[]参数)
{
int[]numaray=新int[48];//48个元素
int total=1;//尝试获取奇数以填充元素
整数指数;

对于(index=0;indexindexnumArray[48]=total
处引发异常,如果它是
则使用赋值运算符:

numArray[i] = y;

这意味着将值y分配给数组numArray

的单元格i,您缺少对数组的赋值。让我们看看下面的代码。这里我们在
中循环查找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
        }
    }
}
使用系统;
名称空间分配4
{
公共课程
{
公共静态void Main(字符串[]args)
{
int upperLimit=100;//将此项添加到循环
int[]numaray=新int[48];//48个元素
int total=1;//尝试获取奇数以填充元素
int指数,i=0;

for(index=0;index)不为for循环中的数组元素赋值。该值为0,因为这是
int[]
元素的默认值,并且您不必费心更改任何元素的值。请参阅复制。
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
        }
    }
}