简体   繁体   English

如何在for循环中为数组赋值

[英]How to assign a value to an Array in a for- loop

First time the loop is being executed: A value is assigned to PlaatSnoepArray[0,0] and PlaatSnoepArray[0,1] . 第一次执行循环:为PlaatSnoepArray[0,0] and PlaatSnoepArray[0,1]分配一个值。

Second time the loop is being executed: A value is assigned to PlaatSnoepArray[1,0] and PlaatSnoepArray[1,1] AND the values of PlaatSnoepArray[0,0] and PlaatSnoepArray[0,1] are set to 0. 第二次执行循环:为PlaatSnoepArray[1,0] and PlaatSnoepArray[1,1]赋值, PlaatSnoepArray[1,0] and PlaatSnoepArray[1,1] PlaatSnoepArray[0,0]PlaatSnoepArray[0,1]的值设置为0。

Third time the loop is being executed: A value is assigned to PlaatSnoepArray[2,0] and PlaatSnoepArray[2,1] . 第三次执行循环:为PlaatSnoepArray[2,0]PlaatSnoepArray[2,1]分配一个值。 AND the values of PlaatSnoepArray[1,0] and PlaatSnoepArray[1,1] are set to 0 . 并且PlaatSnoepArray[1,0]PlaatSnoepArray[1,1]的值设置为0

How can i prevent that the values are set back to 0 ? 如何防止将值设置回0?

    static Random Rangen = new Random();
    static void PlaatsSnoep(int aantal)
    {

        for (int i = 0; i < aantal; i++)
        {

            int SnoepX = Rangen.Next(25, 94);
            int SnoepY = Rangen.Next(3, 23);
            Console.SetCursorPosition(SnoepX, SnoepY);
            Console.WriteLine("0");

            int[,] PlaatssnoepArray = new int[aantal,2];

            PlaatssnoepArray[i, 0] = SnoepX;
            PlaatssnoepArray[i, 1] = SnoepY;

        }

Create the array outside the for loop: for循环外创建array

    int[,] PlaatssnoepArray = new int[aantal,2];

    for (int i = 0; i < aantal; i++)
    {

        int SnoepX = Rangen.Next(25, 94);
        int SnoepY = Rangen.Next(3, 23);
        Console.SetCursorPosition(SnoepX, SnoepY);
        Console.WriteLine("0");

        PlaatssnoepArray[i, 0] = SnoepX;
        PlaatssnoepArray[i, 1] = SnoepY;

    }

How can i prevent that the values are set back to 0 ? 如何防止将值设置回0?

You need to move the creation of the PlaatssnoepArray outside the loop. 您需要在循环外移动PlaatssnoepArray的创建。 Currently, each iteration assigns to its own instane of int[aantal,2] , which goes out of scope and gets thrown away as soon as the loop iteration is over. 目前,每次迭代都会分配给它自己的int[aantal,2]它超出范围并在循环迭代结束后立即被抛弃。

int[,] PlaatssnoepArray = new int[aantal,2];
for (int i = 0; i < aantal; i++)
{
    // The rest of your code
}

Your array declaration is inside the loop, move it outside. 您的数组声明在循环内部,将其移到外面。

int[,] PlaatssnoepArray = new int[aantal,2];
for (int i = 0; i < aantal; i++)
    {
        int SnoepX = Rangen.Next(25, 94);
        int SnoepY = Rangen.Next(3, 23);
        Console.SetCursorPosition(SnoepX, SnoepY);
        Console.WriteLine("0");
        PlaatssnoepArray[i, 0] = SnoepX;
        PlaatssnoepArray[i, 1] = SnoepY;

    }

把你的阵列带出循环..

You are initializing your array inside the loop, move it out 您正在循环中初始化数组,将其移出

static Random Rangen = new Random();
static void PlaatsSnoep(int aantal)
{

    int[,] PlaatssnoepArray = new int[aantal,2];
    for (int i = 0; i < aantal; i++)
    {

        int SnoepX = Rangen.Next(25, 94);
        int SnoepY = Rangen.Next(3, 23);
        Console.SetCursorPosition(SnoepX, SnoepY);
        Console.WriteLine("0");


        PlaatssnoepArray[i, 0] = SnoepX;
        PlaatssnoepArray[i, 1] = SnoepY;

    }
}

And what about using a List<Point>() instead of an array? 那么使用List<Point>()而不是数组呢?

    List<Point> PlaatssnoepList = new List<Point>(); 
    for (int i = 0; i < aantal; i++)
    {
        Point p = new Point(Rangen.Next(25, 94), Rangen.Next(3, 23));
        Console.SetCursorPosition(p.X, p.Y);
        Console.WriteLine("0");
        PlaatssnoepList.Add(p)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM