+ 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; }

23rd Nov 2021, 4:17 PM
Luz
Luz - avatar
6 Answers
+ 4
With this example you can learn that: https://code.sololearn.com/c2Hf4XKQ0QDJ/?ref=app
23rd Nov 2021, 4:38 PM
JaScript
JaScript - avatar
+ 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();
23rd Nov 2021, 5:00 PM
Ipang
+ 3
Luz, Is that the actual snippet? cause I tried it, and see only numbers as I printed the array <vec>.
23rd Nov 2021, 4:51 PM
Ipang
+ 2
it still gives me the same outprint https://code.sololearn.com/cJ1Kc7uL8gWJ/?ref=app
23rd Nov 2021, 4:53 PM
Luz
Luz - avatar
+ 2
Ipang thank youu, I had no idea, now it works
23rd Nov 2021, 5:10 PM
Luz
Luz - avatar