這是我遇到的一個任務。我仍然是Java的新手。我似乎無法加載學生的姓名和成績。名字和等級應該被硬編碼爲主,不一定是25.所以我做了4.我在加載數據時遇到了困難。 (成績)創建一個名爲Student的藍圖,其中包含兩個字段,一個字符串作爲其名稱和一個整數的一維數組,它們是成績(不同學生的成績數可能不同)。至少使用兩個構造函數完成藍圖,一個toString,以及getters和setter。您可以根據需要添加更多方法。 寫,有一個驅動程序,至少,以下結構:如何使用驅動程序類從陣列加載數據
public static void main(String[] args) {
// we do not know the number of students but there will not be >25
Student[] students = new Student[25];
int numStudents = loadStudents(students);
printStudents(students,numStudents);
}
public static int loadStudents(Student[] s) { }
public static void printStudents(Student[] s, int numStudents) {}
}
這是我到目前爲止所。
學生類:
public class Student {
String name;
int[] grades;
public Student()
{
}
public Student(String n,int[] g)
{
name=n;
grades=g;
}
public String toString(){
return "";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int[] getGrades() {
return grades;
}
public void setGrades(int[] grades) {
this.grades = grades;
}
}
driverclass:
所有的public class Driver {
public static void main(String[] args) {
// we do not know the number of students but there will not be >25
Student[] students = new Student[25];
Student s1= new Student("John", new int[]{98, 92, 81});
Student s2= new Student("Claire", new int[]{75, 84, 91, 39});
Student s3= new Student("Steven", new int[]{88, 94, 65, 91,95});
Student s4= new Student("Jason", new int[]{97, 89, 85, 82});
int numStudents = loadStudents(students);
printStudents(students,numStudents);
}
public static int loadStudents(Student[] s) {
for(int i=0;i<4;i++){
s[i]=new Student();
System.out.println(s[i]);
}
}
public static void printStudents(Student[] s, int numStudents) {
if(numStudents < s.length){
System.out.println(s[numStudents]);
printStudents(s,numStudents-1);
}
}
}
如果我們爲您完成這項任務,我們也獲得課程學分嗎? – markspace 2014-08-29 19:08:06
大聲笑我只需要知道如何加載數據,我可以做其餘的。 – SikRik 2014-08-29 19:11:21
那麼,你可能想問問你的老師或TA是什麼意思「加載」的東西。您正在使用「加載」方法打印學生。通常,打印被認爲是輸出,並且加載被稱爲輸入。您必須執行與「加載」相反的打印。 – markspace 2014-08-29 19:13:00