Printing an array of a specific size in java -
i'm trying print out array of 10 objects. reason though, when print out array, there anywhere 15 22 elements in array. can't figure out why. can please point me in right direction?
import java.util.random; public class main { public static void main( string[] args ) { shape[] myshapes = new shape[10]; //array of 10 shapes random rand = new random(); //random number generator int shape, x1, y1, x2, y2, x3, y3; double radius; for( int = 0; < 10; i++ ) { shape = rand.nextint(3); //randomly select shape switch( shape ) { case 0: //triangle x1 = rand.nextint(101); y1 = rand.nextint(101); x2 = rand.nextint(101); y2 = rand.nextint(101); x3 = rand.nextint(101); y3 = rand.nextint(101); myshapes[i] = new triangle( x1, y1, x2, y2, x3, y3 ); system.out.println("triangle: " + myshapes[i].area()); case 1: //rectangle x1 = rand.nextint(101); y1 = rand.nextint(101); x2 = rand.nextint(101); y2 = rand.nextint(101); myshapes[i] = new rectangle( x1, y1, x2, y2 ); system.out.println("rectangle: " + myshapes[i].area()); case 2: //circle radius = rand.nextdouble()*100.0; x1 = rand.nextint(101); y1 = rand.nextint(101); myshapes[i] = new circle( radius, x1, y1 ); system.out.println("circle: " + myshapes[i].area()); } } }
can please utilize break; each case?
your array has 10 elements. puts content in each elements 3 times - because command continues case case. thus, if case 0
right, set 3 shapes , print 3 prints. if case 1
right, set 2 shapes , print 2 prints.
if set break
after each case, on each iteration set 1 shape , print once.
java arrays
No comments:
Post a Comment