Java 构造函数不设置任何值
我开始了一个简单的项目,但遇到了一个问题。 主类Java 构造函数不设置任何值,java,constructor,Java,Constructor,我开始了一个简单的项目,但遇到了一个问题。 主类 public static void main(String[] args){ /* • Ask the user how many new students will be added to the database. • The user should be prompted to enter a name and year for each student. • The student should have a unique 5
public static void main(String[] args){
/*
• Ask the user how many new students will be added to the database.
• The user should be prompted to enter a name and year for each student.
• The student should have a unique 5-digit id, with the first being their grade level.
• The student should have several course options to choose from.
• Each course costs $600 to enroll.
• The student should be able to check their balance and pay tuition.
• The status of the student should show their name, id, courses, and balance.
*/
Scanner input = new Scanner(System.in);
Scanner n = new Scanner(System.in);
int numberOfStudents=0,year;
String firstName,lastName;
System.out.println("How many students will attend this School? ");
//input number of students
System.out.print("Input : ");
numberOfStudents = input.nextInt();
//end
//Input name and year for every student
Student students [] = new Student[numberOfStudents];
Deposit deposit [] = new Deposit[numberOfStudents];
for(int i = 0 ; i < numberOfStudents ;i ++){
students[i]=new Student();
deposit[i]=new Deposit(students[i]);
//It consumes the /n character
input.nextLine();
//
System.out.print("Insert First name : ");
firstName=input.nextLine();
students[i].setFirstName(firstName);
System.out.print("Insert last name : ");
lastName=input.nextLine();
students[i].setLastName(lastName);
System.out.print("Input year :");
year=input.nextInt();
students[i].setYear(year);
//set cash test
students[i].setCash(1000);
//end
}
for(int j = 0 ; j < numberOfStudents ; j++){
System.out.println("Student " + j + " First name " + students[j].getFirstName() + " has " + deposit[j].getBalance());
}
//end
}
当我调用MainSystem.out.println(存款[j].getBalance())时代码>,它说它是0,但我把它放在存款类的构造函数中
此.balance
是否应该具有来自student.getCash()的值?
存款中的checkBalance方法为我提供了在主类student.getCash()中调用它时所需的值。在初始化存款的现金之前,您正在使用student
实例初始化存款,因此它将是0
。初始化相应的学生
后,移动存款
的初始化,您应该可以:
for(int i = 0 ; i < numberOfStudents ;i ++){
students[i]=new Student();
//It consumes the /n character
input.nextLine();
//
System.out.print("Insert First name : ");
firstName=input.nextLine();
students[i].setFirstName(firstName);
System.out.print("Insert last name : ");
lastName=input.nextLine();
students[i].setLastName(lastName);
System.out.print("Input year :");
year=input.nextInt();
students[i].setYear(year);
//set cash test
students[i].setCash(1000);
//end
deposit[i]=new Deposit(students[i]);
}
for(int i=0;i
该值是在您创建存款
对象时设置的,该对象是在您创建新的学生
对象之后立即设置的。此时,Student
实例中的cash
的值为零。你以后再设定。你可以这样做:
public int getBalance() {
return this.student.getCash();
}
不是一次性设置balance
成员,而是每次调用Student
实例上的方法以获取更新的值。我并不是说这是最好的设计,但它似乎是您预期的结果。您的存款构造函数在初始化任何学生字段之前调用getCash。
for(int i = 0 ; i < numberOfStudents ;i ++){
students[i]=new Student();
//It consumes the /n character
input.nextLine();
//
System.out.print("Insert First name : ");
firstName=input.nextLine();
students[i].setFirstName(firstName);
System.out.print("Insert last name : ");
lastName=input.nextLine();
students[i].setLastName(lastName);
System.out.print("Input year :");
year=input.nextInt();
students[i].setYear(year);
//set cash test
students[i].setCash(1000);
//end
deposit[i]=new Deposit(students[i]);
}
public int getBalance() {
return this.student.getCash();
}