gpt4 book ai didi

java - toString() 方法问题

转载 作者:行者123 更新时间:2023-12-03 03:40:13 24 4
gpt4 key购买 nike

我一直在处理这个任务,这里的代码:

public class Student
{
private String fname;
private String lname;
private String studentId;
private double gpa;

public Student(String studentFname,String studentLname,String stuId,double studentGpa)
{
fname = studentFname;
lname = studentLname;
studentId = stuId;
gpa = studentGpa;
}

public double getGpa()
{
return gpa;
}

public String getStudentId()
{
return studentId;
}

public String getName()
{
return lname + ", " + fname;
}
public void setGpa(double gpaReplacement)
{
if (gpaReplacement >= 0.0 && gpaReplacement <= 4.0)
gpa = gpaReplacement;
else
System.out.println("Invalid GPA! Please try again.");
System.exit(0);
}
}

现在我需要创建一个 toString() 方法,该方法返回一个格式如下的字符串:

Name: Wilson, Mary AnnID number: 12345GPA: 3.5

最佳答案

@Override
public String toString() {
return "Name: " + getName() + "\n" +
"ID Number: " + studentId + "\n" +
"GPA: " + gpa;
}

关于java - toString() 方法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2792472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com