0

This is my first attempt at printing out a set of 50 random ints of the range -20 to 20.

int set1 = (int)(Math.random() * (40) + (-20) );
Scanner input = new Scanner(System.in);
for ( int set2 =1; set2 < 20 ; set2 = set1 )
    System.out.print(set2);

Can anyone help my understand where I am going wrong?

4

2 に答える 2

3

A for loop should be made up of a declaration, a condition, and an incrementation. If you had the last part as set2 += set1 then it would work...

You would want to run the for loop 50 times using

for(int i = 0; i < 50; i ++){
    //generate random number here, print here
    int random = (int)(Math.random() * (40) + (-20) );
    System.out.print(random);
}

And in every loop you generate a new number...

于 2012-10-22T00:34:21.073 に答える
3

That's the way to go:

for (int i = 0; i < 50; i++) {
    int random = (int)(Math.random() * (40) + (-20) );
    System.out.print(random);
}
于 2012-10-22T00:40:18.823 に答える