Hi, noobie here. I am trying to place assorted mushrooms into the world. I added different numbers of each type of mushroom to the ArrayList to simulate probability. However, the code would only place the ten initiated objects into the world, regardless of the value of num.
Thanks! (Just started using Greenfoot, thanks to all your help!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public void createM( int num) { ArrayList<Mushroom> mushrooms = new ArrayList<Mushroom>(); for ( int i = 0 ; i< 4 ;i++) mushrooms.add( new Mushroom1()); for ( int i = 0 ; i< 3 ;i++) mushrooms.add( new Mushroom2()); for ( int i = 0 ; i< 2 ;i++) mushrooms.add( new Mushroom3()); mushrooms.add( new Mushroom4()); for ( int i = 0 ; i < num; i++) { Mushroom m = mushrooms.get(( int )(Math.random() * ((mushrooms.size() - 1 ) + 1 )) + 0 ); addObject(m,( int )(Math.random() * ((getWidth() - 0 ) + 1 ) + 0 ), ( int )(Math.random() * ((getHeight() - 0 ) + 1 ) + 0 )); } } |