简体   繁体   English

如何在下一个位置向数组添加值

[英]how to add value to array in the next postion

i am building a small app that act like a digital camera, and on my takephoto function i want to insert a random number to my array(i succeed doing that), my problem is that when i take multiple pictures the value always go into the first postion. 我正在构建一个像数码相机一样的小型应用,并且在我的照相功能上,我想向我的数组中插入一个随机数(我成功做到了),我的问题是当我拍摄多张照片时,该值总是进入第一个位置。 my code: 我的代码:

public override void TakePhoto()
{
    Random rnd = new Random();
    int photo = rnd.Next(1, 10);
    for (int i = 0; i < 1; i++)
    {

        MemoryCard.buffer[i] = photo;
    }
}

class Program
{
    static void Main(string[] args)
    {

        DigitalCamera digitalCamera = 
            new DigitalCamera("kodak", 43, newMemoryCard, 3);

        digitalCamera.TakePhoto();
        digitalCamera.TakePhoto();
        digitalCamera.TakePhoto();

    }
}

how do i jump to the next position after each photo? 每张照片后我如何跳到下一个位置?

You esplicitly say to put next value in the first position. 您毫不客气地说要将下一个值放在第一位。

Look on your code: 查看您的代码:

for (int i = 0; i < 1; i++)
{
   MemoryCard.buffer[i] = photo;
}

i is always 0 i总是0

To resolve this, just save an i value into some global variable, or next index accept like a parameter in the TakePhoto(...) function. 要解决此问题,只需将i值保存到某个全局变量中,或将下一个索引接受为TakePhoto(...)函数中的参数TakePhoto(...)

Example: 例:

int curindex = 0; //GLOBAL VARIABLE 

public override void TakePhoto()
{
   Random rnd = new Random();
   int photo = rnd.Next(1, 10);
   if(curindex < MemoryCard.buffer.Length) //IF CURINDEX LESS THE TOTAL ARRAY LENGTH
   {
      MemoryCard.buffer[curindex] = photo;  //ASSIGN PHOTO IN CURRENTINDEX
      curindex ++;                          //INCEREMENT CURRENTINDEX
   }
}
  1. You have a bug in your for-loop, i assume you want to use the Length of the buffer variable instead 您的for循环中有一个错误,我假设您想使用buffer变量的Length来代替
  2. You are calling TakePhoto too fast, therefore you create the Random always with the same seed, hence always the same "random" number is generated. 您调用TakePhoto速度太快,因此始终使用相同的种子创建Random ,因此始终生成相同的“随机数”。

Instead pass the Random instance as parameter to the method or use a field variable. 而是将Random实例作为参数传递给方法或使用字段变量。

public override void TakePhoto(Random rnd)
{
    int photo = rnd.Next(1, 10);
    for (int i = 0; i < MemoryCard.buffer.Length; i++)
    {

        MemoryCard.buffer[i] = photo;
    }
}

Now always use the same instance: 现在始终使用相同的实例:

Random r = new Random();
DigitalCamera digitalCamera = new DigitalCamera("kodak", 43, newMemoryCard, 3);
digitalCamera.TakePhoto(r);
digitalCamera.TakePhoto(r);
digitalCamera.TakePhoto(r);

I think following modification to your code will resolve your issue: 我认为对代码进行以下修改可以解决您的问题:

class DigitalCamera
{
    static int currentPhotoNumber = 0;
    private Random rnd = new Random();

    public override void TakePhoto()
    {
        int photo = rnd.Next(1, 10);
        MemoryCard.buffer[currentPhotoNumber++] = photo;
    }
}
class Program
{
    static void Main(string[] args)
    {

        DigitalCamera digitalCamera = new DigitalCamera("kodak", 43, newMemoryCard, 3);

        digitalCamera.TakePhoto();
        digitalCamera.TakePhoto();
        digitalCamera.TakePhoto();

    }
}

There is a logic error in your code here: 您的代码中存在逻辑错误:

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

        MemoryCard.buffer[i] = photo;
    }

If you look at your for-loop variables, the loop is running from i=0, to i<1. 如果查看for循环变量,则循环从i = 0到i <1。 Which basically means that the loop is only running once (for i=0). 这基本上意味着该循环仅运行一次(对于i = 0)。

You need to increase i each time, perhaps 您可能需要每次增加i

 MemoryCard.buffer[MemoryCard.numberofphotos] = photo;
 MemoryCard.numberofphotos++;   

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

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