2016-09-29 38 views
0

我已經創建了2個java文件:XCompanyShortlist.java和StudentDemo.java。 XCompanyShortlist.java包含主要方法和所有用戶輸入,如學生註冊號,姓名,學期,GPA,CGPA,分支名稱,分班狀態和實習狀態。爲什麼不顯示子類數據成員的值?

StudentDemo.java具有初始化Reg的超類StudentDemo。編號,名稱,學期,GPA,CGPA使用參數化構造函數,它還包含一個方法display(),它顯示所有信息。

類BranchStudent擴展了StudentDemo類幷包含一個名爲BranchName的額外字符串。該類還包含一個display()方法,該方法在超類中調用display()方法,並打印BranchName。另一個類StudentPlacement包含InternshipStatus,PlacementStatus和一組首選公司列表的變量。

這裏是StudentDemo.java文件代碼:

class StudentDemo { 
    long RegNo; 
    String fname; 
    short sem; 
    float gpa; 
    float cgpa; 

    StudentDemo() { 
     RegNo = 0; 
     fname = ""; 
     sem = 0; 
     gpa = (float) 0.0; 
     cgpa = (float)0.0; 
    } 
    StudentDemo(long RegNo, String fname, short sem, float gpa, float cgpa) { 
     this.RegNo = RegNo; 
     this.fname = fname; 
     this.sem = sem; 
     this.gpa = gpa; 
     this.cgpa = cgpa; 
    } 
    StudentDemo(StudentDemo obj) { 
     RegNo = obj.RegNo; 
     fname = obj.fname; 
     sem = obj.sem; 
     gpa = obj.gpa; 
     cgpa = obj.cgpa; 
    } 
    void display() { 
     System.out.println("------------------------------------------"); 
     System.out.println("Registration No. :"+RegNo); 
     System.out.println("Full Name: "+fname); 
     System.out.println("Semester: "+sem); 
     System.out.println("GPA: "+gpa); 
     System.out.println("CGPA: "+cgpa); 
     System.out.println("------------------------------------------"); 
    } 
} 
class BranchStudent extends StudentDemo { 
    public String BranchName; 
    BranchStudent(long RegNo,String fname,short sem,float gpa,float cgpa,String BranchName) { 
     super(RegNo,fname,sem,gpa,cgpa); 
     this.BranchName = BranchName; 
    } 
    BranchStudent() { 
     super(); 
     BranchName = "CSE"; 
    } 
    BranchStudent(BranchStudent obj) { 
     super(obj); 
     BranchName = obj.BranchName; 
    } 

    void display() { 
     super.display(); 
     System.out.println("Student Branch: "+BranchName); 
    } 
} 
class StudentPlacement extends BranchStudent { 
    String compList[]; 
    int StatusPlacement, StatusIntern; 

    StudentPlacement() { 
     super(); 
     StatusPlacement = 0; 
     StatusIntern = 0; 
     compList = new String[3]; 
    } 

    StudentPlacement(StudentPlacement obj) { 
     super(obj); 
     StatusPlacement = obj.StatusPlacement; 
     StatusIntern = obj.StatusIntern; 
     compList = obj.compList; 
    } 

    StudentPlacement(long RegNo, String fname, short sem, float gpa, float cgpa, String BranchName,String compList[], int StatusPlacement,int StatusIntern) { 
     super(RegNo, fname, sem, gpa, cgpa, BranchName); 
     this.compList = compList; 
     this.StatusPlacement = StatusPlacement; 
     this.StatusIntern = StatusIntern; 
    } 
} 

這裏是XCompanyShortlist.java文件代碼:

import java.util.Scanner; 
public class XCompanyShortlist { 

    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Please Enter The Number Of Students: "); 
     int n = sc.nextInt(); 

     StudentPlacement obj[] = new StudentPlacement[n]; 
     for(int i = 0; i < n; i++) { 
      obj[i] = new StudentPlacement(); 
     } 
     System.out.println("Please Enter The Student Details: "); 
     for(int i = 0; i < n; i++) { 
      System.out.print("Please Enter The Reg. No. :"); 
      long RegNo = sc.nextLong(); 
      sc.nextLine(); 
      System.out.print("Please Enter The Full Name: "); 
      String fname = sc.nextLine(); 
      System.out.print("Please Enter The Semester: "); 
      short sem = sc.nextShort(); 
      System.out.print("Please Enter The GPA: "); 
      float gpa = sc.nextFloat(); 
      System.out.print("Please Enter The CGPA: "); 
      float cgpa = sc.nextFloat(); 
      System.out.print("Please Enter Branch Name:"); 
      String branchName = sc.nextLine(); 
      sc.nextLine(); 
      System.out.println("Please Enter 3 Preferred Choice: "); 
      String compList[] = new String[3]; 
      for(int x = 0; x < 3; x++) { 
       compList[x] = sc.nextLine(); 
      } 
      System.out.print("Please Enter The Status Of Placement(0/1): "); 
      int statusPlacement = sc.nextInt(); 
      System.out.print("Please Enter Status Of Internship(0/1): "); 
      int statusIntern = sc.nextInt(); 
      obj[i] = new StudentPlacement(RegNo,fname,sem,gpa,cgpa,branchName,compList,statusPlacement,statusIntern); 
      System.out.println(); 
     } 
     for(int i = 0; i < n; i++) { 
      obj[i].display(); 
     } 
     sc.close(); 
    } 

} 

我現在面臨的問題是,從StudentDemo的所有學生的詳細資料超類正在展開,但子類BranchStudent未打印分支名稱。我無法在我的代碼中找到問題。

OUTPUT:

Please Enter The Number Of Students: 
1 
Please Enter The Student Details: 
Please Enter The Reg. No. :159101046 
Please Enter The Full Name: Bitan Basak 
Please Enter The Semester: 3 
Please Enter The GPA: 8.86 
Please Enter The CGPA: 8.64 
Please Enter Branch Name:CSE 
Please Enter 3 Preferred Choice: 
HP 
Dell 
Microsoft 
Please Enter The Status Of Placement(0/1): 0 
Please Enter Status Of Internship(0/1): 0 

------------------------------------------ 
Registration No. :159101046 
Full Name: Bitan Basak 
Semester: 3 
GPA: 8.86 
CGPA: 8.64 
------------------------------------------ 
Student Branch: 

這是我的程序給出的輸出。你可以看到學生部門沒有被打印。我無法理解爲什麼。

+0

您是否試圖首次亮相您的代碼? – Spotted

+1

^他的意思是**調試**。 – progyammer

回答

3

從我可以告訴這個問題無關與繼承有關,而是你正在爲構造函數提供一個空行。

這意味着Scanner.nextLine()方法的使用有些問題。如果我將您的代碼更改爲:

public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Please Enter The Number Of Students: "); 
     int n = sc.nextInt(); 

     StudentPlacement obj[] = new StudentPlacement[n]; 
     for(int i = 0; i < n; i++) { 
      obj[i] = new StudentPlacement(); 
     } 
     System.out.println("Please Enter The Student Details: "); 
     for(int i = 0; i < n; i++) { 
      System.out.print("Please Enter The Reg. No. :"); 
      long RegNo = sc.nextLong(); 
      sc.nextLine(); 
      System.out.print("Please Enter The Full Name: "); 
      String fname = sc.nextLine(); 
      System.out.print("Please Enter The Semester: "); 
      short sem = sc.nextShort(); 
      System.out.print("Please Enter The GPA: "); 
      float gpa = sc.nextFloat(); 
      System.out.print("Please Enter The CGPA: "); 
      float cgpa = sc.nextFloat(); 
      sc.nextLine(); 
      System.out.print("Please Enter Branch Name:"); 
      String branchName = sc.nextLine(); 
      System.out.println("Please Enter 3 Preferred Choice: "); 
      String compList[] = new String[3]; 
      for(int x = 0; x < 3; x++) { 
       compList[x] = sc.nextLine(); 
      } 
      System.out.print("Please Enter The Status Of Placement(0/1): "); 
      int statusPlacement = sc.nextInt(); 
      System.out.print("Please Enter Status Of Internship(0/1): "); 
      int statusIntern = sc.nextInt(); 
      obj[i] = new StudentPlacement(RegNo,fname,sem,gpa,cgpa,branchName,compList,statusPlacement,statusIntern); 
      System.out.println(); 
     } 
     for(int i = 0; i < n; i++) { 
      obj[i].display(); 
     } 
     sc.close(); 
    } 

I.e.在分支名稱輸入之前移動sc.nextLine(),掃描儀從控制檯拾取正確的值。

希望有所幫助。

問候

+0

我犯了多麼愚蠢的錯誤。無論如何感謝您的幫助! –

0
空隙顯示

()方法,則調用超顯示()方法,以便超級顯示器()方法正在調用沒有分支的顯示方法,並添加this.branchname

+1

這是錯誤的。 'super.display();'簡單地調用'display();'的父類'版本,然後繼續使用它自己的版本。 – kevto

+0

您可以看到@kevto是正確的,因爲在輸出中將打印標題「'學生科:'」(只有冒號後缺少的實際分支名稱)。 –

相關問題