+ 2
JAVA: Can anyone help me with how to assign random numbers to an array?
Hi, i tried with this, but when I print the array it shows random values, such as letters and symbols too, which is weird since it should be int. I don't know what's wrong. int[] vec = new int[size]; for (int i=0; i<size; i++) { vec[i] = (int) (Math.random()*100)+1; }
6 Answers
+ 4
With this example you can learn that:
https://code.sololearn.com/c2Hf4XKQ0QDJ/?ref=app
+ 4
Luz,
I don't think you can print an array simply like that. I suggest you to use a loop to print an array's content.
for( int value : vec )
{
System.out.print( value + " " );
}
System.out.println();
+ 3
Luz,
Is that the actual snippet? cause I tried it, and see only numbers as I printed the array <vec>.
+ 2
it still gives me the same outprint
https://code.sololearn.com/cJ1Kc7uL8gWJ/?ref=app
+ 2
Ipang thank youu, I had no idea, now it works