2014-11-06 90 views
0

創建數組後如何顯示該數組中的所有細節?這裏是我做了什麼至今..如何顯示存儲在java中的數組信息

import java.util.Scanner; 
 

 

 
public class Main { 
 
\t 
 
\t 
 
\t 
 
\t public static void main(String[] args){ 
 
\t \t 
 
\t \t Student s = new Student("", "", "", 0, 0, 0, 
 
\t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t 
 
\t \t Scanner sc = new Scanner(System.in); 
 
\t \t 
 
\t \t System.out.println("Enter no. of Student"); 
 
\t \t int x = sc.nextInt(); 
 
\t \t 
 
\t \t Student [] studentDetails = new Student[x]; 
 
\t \t 
 
\t \t for(int i = 0; i < x; i++) { 
 
\t \t \t 
 
\t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t System.out.println("enter f name"); 
 
\t \t \t studentDetails[i].setfName(sc.nextLine()); 
 
\t \t \t 
 
\t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter l name"); 
 
\t \t \t \t studentDetails[i].setlName(sc.nextLine()); 
 
\t \t \t \t 
 
\t \t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter Reg no"); 
 
\t \t \t \t studentDetails[i].setStuID(sc.nextLine()); 
 
\t \t \t \t 
 
\t \t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter Prj 1 marks"); 
 
\t \t \t \t studentDetails[i].setProjM1(sc.nextInt()); 
 
\t \t \t \t 
 
\t \t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter ICT 1 Marks "); 
 
\t \t \t \t studentDetails[i].setIctM1(sc.nextInt()); 
 
\t \t \t \t 
 
\t \t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter Active P 1"); 
 
\t \t \t \t studentDetails[i].setActP1(sc.nextInt()); 
 
\t \t \t \t 
 
\t \t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter Code Assg"); 
 
\t \t \t \t studentDetails[i].setCodeAsM(sc.nextInt()); 
 
\t \t \t \t 
 
\t \t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter ICT 2 Marks"); 
 
\t \t \t \t studentDetails[i].setIctM2(sc.nextInt()); 
 
\t \t \t \t 
 
\t \t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter Active P Marks"); 
 
\t \t \t \t studentDetails[i].setActP2(sc.nextInt()); 
 
\t \t \t \t 
 
\t \t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter ICT 3 Marks"); 
 
\t \t \t \t studentDetails[i].setIctM3(sc.nextInt()); 
 
\t \t \t \t 
 
\t \t \t \t studentDetails[i] = new Student("", "", "", 0, 0, 0, 
 
\t \t \t \t   0, 0, 0, 0, 0, 0, 0, 0, 0); 
 
\t \t \t \t System.out.println("enter Active P 3"); 
 
\t \t \t \t studentDetails[i].setActP3(sc.nextInt()); 
 
\t \t 
 
\t \t \t \t System.out.println(Student.toString(studentDetails)); 
 
\t \t \t \t 
 
\t \t 
 
\t } 
 

 

 
} 
 
}

這是學生類,

package cw; 
 

 
public class Student { 
 

 
\t private static \t String fName; 
 
\t private static String lName; 
 
\t private static String stuID; 
 
\t 
 
\t private static int projM1; 
 
\t private static int ictM1; 
 
\t private static int actP1; 
 
\t 
 
\t 
 
\t private static int codeAsM; 
 
\t private static int ictM2; 
 
\t private static int actP2; 
 
\t 
 
\t 
 
\t private static int ictM3; 
 
\t private static int actP3; 
 
\t 
 
\t private static int component1Marks ; 
 
\t private static int component2Marks; 
 
\t private static int component3Marks; 
 
\t private static double moduleMarks; 
 
\t 
 

 

 
public Student(String fName, String lName, String stuID, int projM1, int ictM1, int actP1, 
 
\t \t int codeAsM,int ictM2, int actP2, int ictM3, int actP3, int component1Marks, 
 
\t \t int component2Marks, int component3Marks, int moduleMarks) 
 
{ 
 
\t super(); 
 
\t this.fName = fName; 
 
\t this.lName = lName; 
 
\t this.stuID = stuID; 
 
\t 
 
\t this.projM1 = projM1; 
 
\t this.ictM1 = ictM1; 
 
\t this.actP1 = actP1; 
 
\t 
 
\t 
 
\t 
 
\t this.codeAsM = codeAsM; 
 
\t this.ictM2 = ictM2; 
 
\t this.actP2 = actP2; 
 
\t 
 
\t this.ictM3 = ictM3; 
 
\t this.actP3 = actP3; 
 
\t 
 
} 
 

 

 

 
public String getfName() { 
 
\t return fName; 
 
} 
 

 

 

 
public void setfName(String fName) { 
 
\t this.fName = fName; 
 
} 
 

 

 

 
public String getlName() { 
 
\t return lName; 
 
} 
 

 

 

 
public void setlName(String lName) { 
 
\t this.lName = lName; 
 
} 
 

 

 

 
public String getStuID() { 
 
\t return stuID; 
 
} 
 

 

 

 
public void setStuID(String stuID) { 
 
\t this.stuID = stuID; 
 
} 
 

 

 

 
public int getProjM1() { 
 
\t return projM1; 
 
} 
 

 

 

 
public void setProjM1(int projM1) { 
 
\t this.projM1 = projM1; 
 
} 
 

 

 

 
public int getIctM1() { 
 
\t return ictM1; 
 
} 
 

 

 

 
public void setIctM1(int ictM1) { 
 
\t this.ictM1 = ictM1; 
 
} 
 

 

 

 
public int getActP1() { 
 
\t return actP1; 
 
} 
 

 

 

 
public void setActP1(int actP1) { 
 
\t this.actP1 = actP1; 
 
} 
 

 

 

 
public int getCodeAsM() { 
 
\t return codeAsM; 
 
} 
 

 

 

 
public void setCodeAsM(int codeAsM) { 
 
\t this.codeAsM = codeAsM; 
 
} 
 

 

 

 
public int getIctM2() { 
 
\t return ictM2; 
 
} 
 

 

 

 
public void setIctM2(int ictM2) { 
 
\t this.ictM2 = ictM2; 
 
} 
 

 

 

 
public int getActP2() { 
 
\t return actP2; 
 
} 
 

 

 

 
public void setActP2(int actP2) { 
 
\t this.actP2 = actP2; 
 
} 
 

 

 

 
public int getIctM3() { 
 
\t return ictM3; 
 
} 
 

 

 

 
public void setIctM3(int ictM3) { 
 
\t this.ictM3 = ictM3; 
 
} 
 

 

 

 
public int getActP3() { 
 
\t return actP3; 
 
} 
 

 

 

 
public void setActP3(int actP3) { 
 
\t this.actP3 = actP3; 
 
} 
 

 

 

 
public int getComponent1Marks() { 
 
\t return component1Marks; 
 
} 
 

 

 

 
public void setComponent1Marks(int component1Marks) { 
 
\t this.component1Marks = component1Marks; 
 
} 
 

 

 

 
public int getComponent2Marks() { 
 
\t return component2Marks; 
 
} 
 

 

 

 
public void setComponent2Marks(int component2Marks) { 
 
\t this.component2Marks = component2Marks; 
 
} 
 

 

 

 
public int getComponent3Marks() { 
 
\t return component3Marks; 
 
} 
 

 

 

 
public void setComponent3Marks(int component3Marks) { 
 
\t this.component3Marks = component3Marks; 
 
} 
 

 

 

 
public double getModuleMarks() { 
 
\t return moduleMarks; 
 
} 
 

 

 

 
public void setModuleMarks(double moduleMarks) { 
 
\t this.moduleMarks = moduleMarks; 
 
} 
 

 
public static String toString(Student[] studentDetails) { 
 
\t return "Student [fName=" + fName +", Lname=" + lName + ", studentID=" 
 
\t \t \t + stuID + ", marksProject1=" + projM1 + ", marksICT1=" 
 
\t \t \t + ictM1 + ", ActiveP=" + actP1 + ", Code Ass=" 
 
\t \t \t + codeAsM + ", ICT2=" + ictM2 + ", ActiveP2=" + actP2 + ", ICT3=" + ictM3 + ", ActiveP3=" 
 
\t \t \t + actP3 + "]"; 
 
} 
 
}

當執行程序,它要求輸入

。但是當我輸入值,這是我得到的輸出,

Student [fName =,Lname =,studentID =,marksProject1 = 0,marksICT1 = 0,ActiveP = 0,CodeAss = 0,ICT2 = 0, ActiveP2 = 0,ICT3 = 0,ActiveP3 = 76]

請注意,只有ActiveP3值才能正確打印!

如何糾正? 我是新來的java.this是我的第一項任務。

+0

當您最初創建Student對象時,請使用默認構造函數(不帶參數的構造函數,您需要編寫一個參數),然後在提供每個值時,使用該方法設置該值。然後將Student對象分配給數組中的第i個位置。你現在正在做的是每次用一個只有用戶提供的最後一個值的新對象覆蓋對象狀態。因此,您看到的唯一用戶值是ActiveP3值。 – 2014-11-06 07:17:17

回答

0

在每次迭代中創建只有一個新對象,並用一個空的構造爲葉蘭建議:

import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 

     System.out.println("Enter no. of Student"); 

     int numberOfStudents = sc.nextInt(); 
     Student[] studentDetails = new Student[numberOfStudents]; 

     for (int i = 0; i < numberOfStudents; i++) { 
      Student newStudent = new Student(); 

      System.out.println("enter f name"); 
      newStudent.setfName(sc.next()); 
      System.out.println("enter l name"); 
      newStudent.setlName(sc.next()); 
      System.out.println("enter Reg no"); 
      newStudent.setStuID(sc.next()); 
      System.out.println("enter Prj 1 marks"); 
      newStudent.setProjM1(sc.nextInt()); 
      System.out.println("enter ICT 1 Marks "); 
      newStudent.setIctM1(sc.nextInt()); 
      System.out.println("enter Active P 1"); 
      newStudent.setActP1(sc.nextInt()); 
      System.out.println("enter Code Assg"); 
      newStudent.setCodeAsM(sc.nextInt()); 
      System.out.println("enter ICT 2 Marks"); 
      newStudent.setIctM2(sc.nextInt()); 
      System.out.println("enter Active P Marks"); 
      newStudent.setActP2(sc.nextInt()); 
      System.out.println("enter ICT 3 Marks"); 
      newStudent.setIctM3(sc.nextInt()); 
      System.out.println("enter Active P 3"); 
      newStudent.setActP3(sc.nextInt()); 

      studentDetails[i] = newStudent; 

      System.out.println(newStudent); 
     } 

     sc.close(); 
    } 

} 

空構造添加到您的Student類,不要使變量static和修改toString()方法:

public class Student { 

    private String fName; 
    private String lName; 
    private String stuID; 
    private int projM1; 
    private int ictM1; 
    private int actP1; 
    private int codeAsM; 
    private int ictM2; 
    private int actP2; 
    private int ictM3; 
    private int actP3; 
    private int component1Marks; 
    private int component2Marks; 
    private int component3Marks; 
    private double moduleMarks; 

    public Student() { 
    } 

    . 
    . 
    . 

    @Override 
    public String toString() { 
     return "Student [fName=" + fName + ", Lname=" + lName + ", studentID=" + stuID + ", marksProject1=" + projM1 
       + ", marksICT1=" + ictM1 + ", ActiveP=" + actP1 + ", Code Ass=" + codeAsM + ", ICT2=" + ictM2 
       + ", ActiveP2=" + actP2 + ", ICT3=" + ictM3 + ", ActiveP3=" + actP3 + "]"; 
    } 

} 
0

你只應該調用一次學生構造器陣列中的每個條目:

studentDetails[i] = new Student("", "", "", 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0); 

多次調用它的i相同的值覆蓋存儲在的的i個位置的前一個參考陣列。

並沒有多大意義可以調用這麼多參數的構造函數來傳遞所有參數的默認值。使用無參數的構造函數。

此外,Student類的toString()方法不需要任何參數,因爲它只是打印它所要求的單個Student的屬性。

,則應該更換電話:

System.out.println(Student.toString(studentDetails)); 

System.out.println(studentDetails[i].toString()); 

你的循環應該是這樣的:

for(int i = 0; i < x; i++) { 

      // this can be replaced by studentDetails[i] = new Student(); 
      studentDetails[i] = 
       new Student("", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 

      System.out.println("enter f name"); 
      studentDetails[i].setfName(sc.nextLine()); 

      System.out.println("enter l name"); 
      studentDetails[i].setlName(sc.nextLine()); 

      System.out.println("enter Reg no"); 
      studentDetails[i].setStuID(sc.nextLine()); 

      System.out.println("enter Prj 1 marks"); 
      studentDetails[i].setProjM1(sc.nextInt()); 

      System.out.println("enter ICT 1 Marks "); 
      studentDetails[i].setIctM1(sc.nextInt()); 

      System.out.println("enter Active P 1"); 
      studentDetails[i].setActP1(sc.nextInt()); 

      System.out.println("enter Code Assg"); 
      studentDetails[i].setCodeAsM(sc.nextInt()); 

      System.out.println("enter ICT 2 Marks"); 
      studentDetails[i].setIctM2(sc.nextInt()); 

      System.out.println("enter Active P Marks"); 
      studentDetails[i].setActP2(sc.nextInt()); 

      System.out.println("enter ICT 3 Marks"); 
      studentDetails[i].setIctM3(sc.nextInt()); 

      System.out.println("enter Active P 3"); 
      studentDetails[i].setActP3(sc.nextInt()); 

      System.out.println(studentDetails[i]);  
    } 

,改變你的toString方法:

public String toString() {...} 
+0

我明白覆蓋的東西,但我怎麼能避免它?如何使用無參數構造函數或我應該在代碼中編輯多少次以停止調用它? – Raveen 2014-11-06 07:18:17

+0

@Raveen看我的編輯 – Eran 2014-11-06 07:24:29

0
public class Main { 

    public static void main(String[] args){ 

     Student student = null; 

     Scanner sc = new Scanner(System.in); 

     System.out.println("Enter no. of Student"); 
     int x = sc.nextInt(); 

     Student [] studentDetails = new Student[x]; 

     for(int i = 0; i < x; i++) { 

      student = new Student("", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 

      System.out.println("enter f name"); 
      student.setfName(sc.nextLine()); 

      System.out.println("enter l name"); 
      student.setlName(sc.nextLine()); 

      System.out.println("enter Reg no"); 
      student.setStuID(sc.nextLine()); 

      System.out.println("enter Prj 1 marks"); 
      student.setProjM1(sc.nextInt()); 

      System.out.println("enter ICT 1 Marks "); 
      student.setIctM1(sc.nextInt()); 

      System.out.println("enter Active P 1"); 
      student.setActP1(sc.nextInt()); 

      System.out.println("enter Code Assg"); 
      student.setCodeAsM(sc.nextInt()); 

      System.out.println("enter ICT 2 Marks"); 
      student.setIctM1(sc.nextInt()); 

      System.out.println("enter Active P Marks"); 
      student.setActP1(sc.nextInt()); 

      System.out.println("enter ICT 3 Marks"); 
      student.setIctM3(sc.nextInt()); 

      System.out.println("enter Active P 3"); 
      student.setActP1(sc.nextInt()); 

      student = new Student(student.getfName(), student.getlName(), student.getStuID(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 

      studentDetails[i] = student; 

      System.out.println(Student.toString(studentDetails)); 


     } 
    } 
} 

當你正在創建學生對象的數組設置的值Student對象,並添加對象以學生陣列