This site requires JavaScript, please enable it in your browser!
Greenfoot back
n1ght5t41xX
n1ght5t41xX wrote ...

2019/4/25

Adding numbers to an arrays content

n1ght5t41xX n1ght5t41xX

2019/4/25

#
Hello, I am trying to pick a random array out of 6, and everytime one array is picked, add one number to it (so i have a total count afterwards) How can i do that? Heres what i did so far:
1
2
3
4
5
6
7
8
9
10
11
12
int []dice = {0,0,0,0,0,0};
    int n;
     
    public void act()
    {
        for (int i = 0; i <= 100; i++)
        {
         n = Greenfoot.getRandomNumber(6)+1;
         dice[n] = ???
          
        }
    }
I dont think this is complicated, i just dont have the drill yet... thank you.
danpost danpost

2019/4/25

#
If I understand correctly, at the end, the sum of all elements will be 100. Remove the '+1' from line 8 and use one of the following for line 9:
1
2
3
4
5
dice[n]++;
// or
dice[n] += 1;
// or
dice[n] = dice[n]+1;
n1ght5t41xX n1ght5t41xX

2019/4/25

#
danpost wrote...
If I understand correctly, at the end, the sum of all elements will be 100. Remove the '+1' from line 8 and use one of the following for line 9:
1
2
3
4
5
dice[n]++;
// or
dice[n] += 1;
// or
dice[n] = dice[n]+1;
thank you. have a nice day
You need to login to post a reply.