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

2020/6/2

Help with arrays and for loop

1
2
Sammyy2323 Sammyy2323

2020/6/2

#
I need to take the random array number and turn it into that many balls added to the world. I have not defined any variable and I need help on how to do this for my final project. (Ex. if the array chooses the number 8, 8 balls would be placed in the world) public void CreateBalls() { int num = new int ; i num = Greenfoot.getRandomNumber(5-10); num = Greenfoot.getRandomNumber(5-10); num = Greenfoot.getRandomNumber(5-10); num = Greenfoot.getRandomNumber(5-10); for (int i= 0; i<num.length;i++) { } }
danpost danpost

2020/6/2

#
You mean this?
for (int i=0; i<5+Greenfoot.getRandomNumber(6); i++)
{

}
danpost danpost

2020/6/2

#
Oh, this is different:
Sammyy2323 wrote...
public void CreateBalls()
{
    int [] num = new int [4];
    i
    num [0] = Greenfoot.getRandomNumber(5-10); 
    num [1] = Greenfoot.getRandomNumber(5-10);
    num [2] = Greenfoot.getRandomNumber(5-10);
    num [3] = Greenfoot.getRandomNumber(5-10);
    for (int i= 0; i<num.length;i++) 
    {
       
    }
}
Where are you using the numbers in the array? As is, your loop will iterate exactly 4 times regardless of the random numbers chosen (because that is the length you set the array to on line 3). Why do you need 4 random values?
Sammyy2323 Sammyy2323

2020/6/2

#
i want to have 4 different color balls in the world and say is for a gold ball, the random number would place say 6 balls in the world. and for it is a different color and that will output say 7 balls in the world. Not sure on how to do this.
Sammyy2323 Sammyy2323

2020/6/2

#
the 0 in the array will stand for a gold ball and 1 in the array is for blue and so on
danpost danpost

2020/6/2

#
Sammyy2323 wrote...
i want to have 4 different color balls in the world and say is for a gold ball, the random number would place say 6 balls in the world. and for it is a different color and that will output say 7 balls in the world. Not sure on how to do this.
Okay, makes a little more sense now. You will need a loop inside the loop you already have, counter based on random values:
for (int i=0; i<num.length; i++)
{
    for (int n=0; n<num[i]; n++)
    {

    }
}
Sammyy2323 Sammyy2323

2020/6/2

#
what would i put inside the inner loop? I understand that I need another loop inside of it but what would I put in it?
danpost danpost

2020/6/2

#
Sammyy2323 wrote...
what would i put inside the inner loop? I understand that I need another loop inside of it but what would I put in it?
I presume you would be creating balls, the type of which would depend on i, and add them to the world.
Sammyy2323 Sammyy2323

2020/6/2

#
so would i put in the outer loop the random colors and in the inner loop I create that color ball?
danpost danpost

2020/6/2

#
Sammyy2323 wrote...
so would i put in the outer loop the random colors and in the inner loop I create that color ball?
I would suggest the color be given by the Ball constructor. You would only need to pass its type value. At line 5 above:
Ball = new Ball(i);
// addObject line to follow
An example Ball class might be along these lines (incomplete):
public class Ball extends Actor
{
    private static final String[] COLORS = { "red", ... } // whatever the order is
    private int value;
    
    public Ball(int id)
    {
        value = id;
        setImage(new GreenfootImage(COLORS[value]+"-ball.png"));
    }
    
    public String getColor()
    {
        return COLORS[value];
    }
}
Sammyy2323 Sammyy2323

2020/6/2

#
ok so this will create the random color. Gotcha. But how would I make an int array with the random numbers and turn that into how many balls? Or did you already do that and I don't get this?
danpost danpost

2020/6/2

#
Sammyy2323 wrote...
ok so this will create the random color. Gotcha. But how would I make an int array with the random numbers and turn that into how many balls? Or did you already do that and I don't get this?
I am not sure you get it or not. Your 13 line code post, modified with my 7 line code post with the line 5 insertion would be the end result for the CreateBalls method
Sammyy2323 Sammyy2323

2020/6/2

#
so i combine my first 13 line code and your 7 line code and add a little and the code will place a random amount of balls in the world and also a random color correct?
danpost danpost

2020/6/2

#
Sammyy2323 wrote...
so i combine my first 13 line code and your 7 line code and add a little and the code will place a random amount of balls in the world and also a random color correct?
It will place 5 to 10 of each of 4 distinguishably different balls in the world. The Ball class, per my suggestion, would provide the proper color to each Ball instance depending on the value provided it.
Sammyy2323 Sammyy2323

2020/6/2

#
ok, and I would keep the CreateBalls method and then make the Ball method also? (sorry for asking questions, final project is due in like 2 days and I need to make sure this is correct)
There are more replies on the next page.
1
2