构造函数不设置任何值

huangapple go评论74阅读模式
英文:

Constructor doesnt set any values

问题

我开始了一个简单的项目但是遇到了一个问题
主类

public static void main(String[] args){

    /*
    • 要求用户输入要添加到数据库的新学生数量。
    • 用户应该被提示输入每个学生的姓名和年级。
    • 学生应该有一个唯一的5位数ID,第一个数字是他们的年级。
    • 学生应该有几个课程选项可供选择。
    • 每个课程的注册费用为$600。
    • 学生应该能够查看他们的余额并支付学费。
    • 学生的状态应显示他们的姓名、ID、课程和余额。
         */
    Scanner input = new Scanner(System.in);
    Scanner n = new Scanner(System.in);
    int numberOfStudents=0,year;
    String firstName,lastName;

    System.out.println("有多少学生将参加这个学校?");
    //输入学生数量
    System.out.print("输入: ");
    numberOfStudents = input.nextInt();
    //结束
    //为每个学生输入姓名和年级
    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]);
        //消耗/n字符
        input.nextLine();
        //
        System.out.print("输入名字: ");
        firstName=input.nextLine();
        students[i].setFirstName(firstName);

        System.out.print("输入姓氏: ");
        lastName=input.nextLine();
        students[i].setLastName(lastName);

        System.out.print("输入年级: ");
        year=input.nextInt();
        students[i].setYear(year);
        //设置现金测试
        students[i].setCash(1000);
        //结束
    }
    for(int j = 0 ; j < numberOfStudents ; j++){
        System.out.println("学生 " + j + " 姓氏 " + students[j].getFirstName() + " 余额 " + deposit[j].getBalance());
    }

    //结束
}

学生类

private String firstName,lastName;
private int year,grade,cash;
private int[] studentID = new int[5];

public Student(){

}

public Student(String firstName, String lastName, int year) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.year = year;
}

public Student(String firstName, String lastName, int year, int grade, int cash) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.year = year;
    this.grade = grade;
    this.cash=cash;
}

private int[] RandomID(){
    Random rand = new Random();
    this.studentID[0] = this.grade;
    for(int i = 1; i <= this.studentID.length-1 ; i ++ ){
        int randomNumbers = rand.nextInt(10);

        this.studentID[i] = randomNumbers;
    }
    return this.studentID;
}

public int[] getStudentID() {
    return RandomID();
}

public int getCash() {
    return cash;
}

public void setCash(int cash) {
    this.cash=cash;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public int getGrade() {
    return grade;
}

public void setGrade(int grade) {
    this.grade = grade;
}

存款类

private Student student;
private int balance;

public Deposit(){

}

public Deposit(Student student){
    this.student = student;
    this.balance=student.getCash();
}

public void checkBalance(){
    System.out.println("你在这个账户上有 " + this.student.getCash() + " 美元");
}


public Student getStudent() {
    return student;
}

public void setStudent(Student student) {
    this.student = student;
}

public int getBalance() {
    return this.balance;
}

public void setBalance(int balance) {
    this.balance = balance;
}

在主类中调用System.out.println(deposit[j].getBalance());时,它显示为0,但是我在Deposit类的构造函数中设置了它。

难道this.balance不应该从student.getCash()获得一个值吗?
在主类中调用存款类的checkBalance方法时,它会给我需要的值,即student.getCash()的值。

英文:

I started a simple project, but I've encountered a problem.
Main class

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(&quot;How many students will attend this School? &quot;);
//input number of students
System.out.print(&quot;Input : &quot;);
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 &lt; numberOfStudents ;i ++){
students[i]=new Student();
deposit[i]=new Deposit(students[i]);
//It consumes the /n character
input.nextLine();
//
System.out.print(&quot;Insert First name : &quot;);
firstName=input.nextLine();
students[i].setFirstName(firstName);
System.out.print(&quot;Insert last name : &quot;);
lastName=input.nextLine();
students[i].setLastName(lastName);
System.out.print(&quot;Input year :&quot;);
year=input.nextInt();
students[i].setYear(year);
//set cash test
students[i].setCash(1000);
//end
}
for(int j = 0 ; j &lt; numberOfStudents ; j++){
System.out.println(&quot;Student &quot; + j + &quot; First name &quot; + students[j].getFirstName() + &quot; has &quot; + deposit[j].getBalance());
}
//end
}

Student class

private String firstName,lastName;
private int year,grade,cash;
private int[] studentID = new int[5];
public Student(){
}
public Student(String firstName, String lastName, int year) {
this.firstName = firstName;
this.lastName = lastName;
this.year = year;
}
public Student(String firstName, String lastName, int year, int grade, int cash) {
this.firstName = firstName;
this.lastName = lastName;
this.year = year;
this.grade = grade;
this.cash=cash;
}
private int[] RandomID(){
Random rand = new Random();
this.studentID[0] = this.grade;
for(int i = 1; i &lt;= this.studentID.length-1 ; i ++ ){
int randomNumbers = rand.nextInt(10);
this.studentID[i] = randomNumbers;
}
return this.studentID;
}
public int[] getStudentID() {
return RandomID();
}
public int getCash() {
return cash;
}
public void setCash(int cash) {
this.cash=cash;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}

Deposit class:

private Student student;
private int balance;
public Deposit(){
}
public Deposit(Student student){
this.student = student;
this.balance=student.getCash();
}
public void checkBalance(){
System.out.println(&quot;You have &quot; + this.student.getCash() + &quot; on this account&quot;);
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public int getBalance() {
return this.balance;
}
public void setBalance(int balance) {
this.balance = balance;
}

When I call in Main System.out.println(deposit[j].getBalance());, it says it's 0, but I put it in the constructor in the Deposit class.

Shouldn't this.balance have a value from student.getCash()?
The checkBalance method in deposit gives me the value that I need when I call it in the main class student.getCash().

答案1

得分: 2

你在初始化Deposit之前使用了Student实例进行初始化,因此其cash将为0。将Deposit的初始化移到相应的Student初始化完成之后,问题就应该解决了:

for (int i = 0; i < numberOfStudents; i++) {
    students[i] = new Student();
    //消耗换行符
    input.nextLine();
    //
    System.out.print("输入名字:");
    firstName = input.nextLine();
    students[i].setFirstName(firstName);

    System.out.print("输入姓氏:");
    lastName = input.nextLine();
    students[i].setLastName(lastName);

    System.out.print("输入年份:");
    year = input.nextInt();
    students[i].setYear(year);
    //设置现金测试
    students[i].setCash(1000);
    //结束

    deposit[i] = new Deposit(students[i]);
}
英文:

You're initializing the Deposit with a Student instance before you initialize its cash, so it'll be 0. Move the initialization of the Deposit after you're done initializing the corresponding Student and you should be OK:

for(int i = 0 ; i &lt; numberOfStudents ;i ++){
students[i]=new Student();
//It consumes the /n character
input.nextLine();
//
System.out.print(&quot;Insert First name : &quot;);
firstName=input.nextLine();
students[i].setFirstName(firstName);
System.out.print(&quot;Insert last name : &quot;);
lastName=input.nextLine();
students[i].setLastName(lastName);
System.out.print(&quot;Input year :&quot;);
year=input.nextInt();
students[i].setYear(year);
//set cash test
students[i].setCash(1000);
//end
deposit[i]=new Deposit(students[i]);
}

答案2

得分: 1

这个值是在创建一个Deposit对象的时候设置的,这是在创建一个新的Student对象之后立即发生的。那时,Student实例中的cash的值是零。你直到后来才设置它。你可以这样做:

public int getBalance() {
    return this.student.getCash();
}

与一次性设置一个balance成员变量不同,这将在每次调用时从Student实例中获取更新后的值。我并不是说这是最佳设计,但这似乎是你期望发生的事情。

英文:

The value is being set at the time you create a Deposit object, which is immediately after you create a new Student object. At that time, the value of cash in the Student instance is zero. You don't set that until later. You could do something like:

public int getBalance() {
return this.student.getCash();
}

Instead of setting a balance member once, this would call the method on the Student instance each time to get the updated value. I'm not saying this is the best design, but it appears to be what you expected to happen.

huangapple
  • 本文由 发表于 2020年8月30日 22:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63658318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定