java - Changing the variable assigned everytime a loop iterates -
i trying build program allows user enter length , width of object many times choose (i build more code choose how many times loop goes). having problems figuring out how input , construct object every time loop iterates. thanks!
public static void main(string[] args) { scanner console = new scanner(system.in); (int = 1; <= 2; i++) { system.out.println("enter length" + + ": "); int length = console.nextint(); system.out.println("enter length" + + ": "); int width = console.nextint(); object instance1 = new object(length1, width1); } }
you can use arraylist/linklist, if entries big go linklist.
scanner console = new scanner(system.in); system.out.println("enter how many records want: "); int j = console.nextint(); //"loop run "+ j +" times" list<objectname> objectlist = new arraylist<objectname>(); int length = 0; int width = 0; (int = 1; <= j; i++) { system.out.println("enter length" + + ": "); length = console.nextint(); system.out.println("enter width" + + ": "); width = console.nextint(); objectname instance1 = new objectname(length, width); objectlist.add(instance1); }
Comments
Post a Comment