简体   繁体   中英

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

To resolve this, just save an i value into some global variable, or next index accept like a parameter in the TakePhoto(...) function.

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
  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.

Instead pass the Random instance as parameter to the method or use a field variable.

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. Which basically means that the loop is only running once (for i=0).

You need to increase i each time, perhaps

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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