我会为您的代码建议一个带有注释和 TODO 的高级结构,您可以填写详细信息。最后一部分,我建议newGame
方法的结构,将帮助您摆脱遇到的错误。
类的结构ApplicantInfo
:
public class ApplicantInfo {
private int ID;
private double quality;
// Constructor to create an instance with the specified ID value
public ApplicantInfo(int id){
// TODO: Initialize the value for ID field
}
// Method to get the value for ID
public int getID(){
// TODO: return value of ID field
}
// Method to set the value for ID
public void setID(int id){
// TODO: set the value for ID field
}
// Getter and setter methods for "quality"
// on the lines of the above methods
}
类的结构AllApplicants
:
public class AllApplicants {
private ApplicantInfo[] applicantArr = new ApplicantInfo[20];
// Method to get the applicant info at a given index
public ApplicantInfo getApplicant(int index){
// TODO: Get the applicant from the array present at the specified index
}
// Method to add an applicant info at a given index
public boolean addApplicant(ApplicantInfo applicant, int index){
// TODO: Try to add the specified applicant to the array at the specified index
// Return true to indicate that the applicant was successfully added,
// Return false to indicate that an applicant is already present at the specified index
}
}
因为这只是newGame
方法结构的骨架:
public void newGame {
AllApplicants applicants = new AllApplicants();
// In order to achieve doing "applicants.applicantArr[0].Id = 5;", you
// need to do the following.
// Create a new applicant info with ID as 5
ApplicantInfo applicant = new ApplicantInfo(5);
// Add the applicant to the applicant array at index 0
applicants.addApplicant(applicant, 0);
}
除了阅读@codeman 提到的数组之外,您可能还想看看Java Naming Convention