java - Set large array of numbers to designated length per line -
int size = 50; // generates non-duplicated random numbers int[] values = new int[51]; int[] list = new int[size]; for( int = 0; < 51; i++ ) values[i] = i; random rand = new random(); int listsize = 0; int mylist = 0; while( true ) { int value = rand.nextint(51); if( values[ value ] == 0 ) continue; // number used list[ mylist++ ] = value; values[ value ] = 0; if( mylist == size || mylist == 50 ) break; } // displays non-duplicated random generated numbers for(int element : list) system.out.print(element + " ");
i set large array of numbers designated length per line make numbers appear though they're in block left , right sides so:
> 1 2 3 4 5 6 7 8 9 10 > 11 12 13 14 15 16 17 18 19 20 > 21 22 23 24 25 26 27 28 29 30 > 31 32 33 34 35 36 37 38 39 40 > 41 42 43 44 45 46 47 48 49 50
how accomplish using enhanced loop? help!
add new line after read 10 value
for(int i=0; i<list.length; i++){ system.out.printf(" %-2d",list[i]); //format left justified if ((i+1)%10==0){ system.out.println(); // add new line after 10 } }
Comments
Post a Comment