0
[OK]を、ので、これは私の主な方法の一部です:オブジェクトの配列が上書きされないようにするにはどうすればよいですか?
public class Driver {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
//Variables to place data attributes of object in
String teacherName;
int teacherId;
double basicSalary;
double extraPaymentRate;
int numOfCourses=100;
Course []coursesTaught=new Course[numOfCourses];
String courseName;
int courseId;
int y;
System.out.println("Please enter number of teachers:");
y=scan.nextInt();
Teacher []teacherArray=new Teacher[y];
for(int i=0;i<y;i++) {
System.out.println("Enter teacher’s name, id, basic salary, and extra payment rate: ");
teacherName=scan.next();
teacherId=scan.nextInt();
basicSalary=scan.nextDouble();
extraPaymentRate=scan.nextDouble();
System.out.println("Enter number of courses taught: ");
numOfCourses=scan.nextInt();
for(int j=0;j<numOfCourses;j++) {
System.out.println("Enter course names and IDs:");
courseName=scan.next();
courseId=scan.nextInt();
coursesTaught[j]=new Course(courseName,courseId);
}
teacherArray[i]=new Teacher(teacherName,teacherId,basicSalary,extraPaymentRate,numOfCourses,coursesTaught);
}
今私の問題は、私は新しい先生のオブジェクトを追加するたびに、私はその先生のオブジェクトに追加する新しいコースが最後のものをオーバーライドすることです。 ので、すべての教師が同じコース
を持ってここに先生のクラスは
public class Teacher {
private String name;
private int id;
private double basicSalary;
private double extraPaymentRate;
public static int numOfCourses;
public Course []coursesTaught= new Course[numOfCourses];
public Teacher() {
}
public Teacher(String name,int id,double basicSalary,double
extraPaymentRate,int numOfCourses,Course []coursesTaught) {
this.name=name;
this.id=id;
this.basicSalary=basicSalary;
this.extraPaymentRate=extraPaymentRate;
Teacher.numOfCourses=numOfCourses;
this.coursesTaught=coursesTaught;
}
//setter and getter methods here
だと、ここでコースのクラスは、私はまだ、オブジェクトの概念を把握してい
public class Course {
public String name;
public int id;
public Course() {
}
public Course(String name,int id) {
this.name=name;
this.id=id;
}
です。どんな助け?
あなたは1つの 'coursesTaught'配列を持っています。あなたはそれぞれの先生のために1つ必要です。 –