Reputation: 25
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?
Upvotes: 0
Views: 12402
Reputation: 20557
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...
Upvotes: 3
Reputation: 37813
That's the way to go:
for (int i = 0; i < 50; i++) {
int random = (int)(Math.random() * (40) + (-20) );
System.out.print(random);
}
Upvotes: 3