java - Initiating objects in a for loop -
public static int askingamount() { system.out.println("how many persons there in company?"); scanner amounts = new scanner(system.in); amount = amounts.nextint(); system.out.println(amount); amounts.close(); return amount; } public static void makingpersons() { (int i=0 ; i<amount ; i++) { int personnumber=0; person person[i] = new person(); //<--- problem system.out.println("person"); } }
in first method trying ask user how many persons , return amount. in second wanted create equal amount of person objects , name them person1, person2, person3 using variable "i" don't work. clues?
define array outside loop class member:
person[] person;
then in askingamount
initialize it:
person = new person[amount];
and inside loop do:
person[i] = new person();
Comments
Post a Comment