2016-10-22 103 views
0

我是一名學生,正在尋找幫助完成作業。這是任務:創建一個CollegeCourse類。該課程包含課程編號(例如「CIS 210」),學分(例如3)和字母等級(例如「A」)等字段。Java將類數組傳入數組

包括每個字段的get()和set()方法。創建一個包含一個ID號的學生類和一個由五個CollegeCourse對象組成的數組。爲學號ID創建一個get()和set()方法。還要創建一個返回學生的CollegeCourses之一的get()方法;該方法接受一個整數參數並返回該位置的CollegeCourse(0到4)。接下來,創建一個set()方法來設置一個Student's CollegeCourses的值;該方法需要兩個參數 - 一個CollegeCourse和一個表示CollegeCourse的位置的整數(0到4)。

我得到了第二個for循環的運行時錯誤,我試圖將數據放入課程數組中。它要求同一行中的CourseID和小時數,並且不管我對它做出什麼響應,我收到一個錯誤,它幾乎看起來像試圖在同一時間獲取所有數組變量。這是我的代碼,其中包括三個類。任何幫助我向正確的方向發送我的讚賞,因爲我已經花了很長時間已經研究解決。

public class CollegeCourse { 

private String courseId; 
private int creditHours; 
private char grade; 
public CollegeCourse(String id, int hours, char grade) 
{ 
    courseId=id; 
    creditHours = hours; 
    this.grade = grade; 
} 

public void setCourseId(String id) 
{ 
    courseId = id;//Assign course id to local variable 
} 

public String getCourseId() 
{ 
    return courseId;//Provide access to course id 
} 

public void setHours(int hours) 
{ 
    creditHours = hours;//Assign course id to local variable 
} 

public int getHours() 
{ 
    return creditHours;//Provide access to course id 
} 

public void setGrade(char grade) 
{ 
    this.grade = grade;//Assign course id to local variable 
} 

public char getGrade() 
{ 
    return grade;//Provide access to course id 
} 
} 

學生班級

public class Student { 

final int NUM_COURSES = 5; 

private int studentId; 
private CollegeCourse courseAdd;//Declares a course object 
private CollegeCourse[] courses = new CollegeCourse[NUM_COURSES]; 

//constructor using user input 
public Student(int studentId) 
{ 
    this.studentId=studentId; 
} 

public void setStudentId(int id) 
{ 
    studentId = id;//Assign course id to local variable 
} 

public int getStudentId() 
{ 
    return studentId;//Provide access to course id 
} 

public void setCourse(int index, CollegeCourse course) 
{ 
    courses[index] = course; 
} 

public CollegeCourse getCourse(int index) 
{ 
    return courses[index]; 
    //do I need code to return the courseId hours, grade 
}  
} 

InputGrades類

import java.util.Scanner; 
public class InputGrades { 

public static void main(String[] args) { 

    final int NUM_STUDENTS = 2; 
    final int NUM_COURSES = 3; 

    Student[] students = new Student[NUM_STUDENTS]; 
    int s;//subscript to display the students 
    int c;//subscript to display courses 
    int stId; 
    int csIndex; 
    String courseId = ""; 
    int hours = 0; 
    //String gradeInput; 
    char grade = 'z'; 
    CollegeCourse course = new CollegeCourse(courseId,hours, grade);//not sure if I am handling this correctly 

    Scanner input = new Scanner(System.in); 

    for(s = 0; s<NUM_STUDENTS; ++s) 
    { 
     students[s] = new Student(s); 
     System.out.print("Enter ID for student #" + (s+1) + ":"); 
     stId = input.nextInt(); 
     input.nextLine(); 
     students[s].setStudentId(stId); 
     for(c=0; c < NUM_COURSES; ++c) 
     {    
      csIndex=c; 
      System.out.print("Enter course ID #" + (c+1) + ":"); 
      courseId = input.nextLine(); 
      course.setCourseId(courseId); 
      System.out.print("Enter hours:"); 
      hours = input.nextInt(); 
      input.nextLine(); 
      course.setHours(hours); 
      String enteredGrade = ""; 
       while(enteredGrade.length()!=1) { 
        System.out.print("Enter grade:"); 
        enteredGrade = input.nextLine(); 
        if(enteredGrade.length()==1) { 
         grade = enteredGrade.charAt(0); 
        } else { 
         System.out.println("Type only one character!"); 
        } 
       } 
      course.setGrade(grade); 
      students[s].setCourse(csIndex, course);   
     } 
    } 

    for(s = 0; s<NUM_STUDENTS; ++s) 
    { 
     System.out.print("\nStudent# " + 
       students[s].getStudentId()); 
     System.out.println(); 
     for(c=0;c<NUM_COURSES;++c) 
      System.out.print(students[s].getCourse(c) + " "); 
     System.out.println(); 
    } 
} 

} 
+0

你有什麼錯誤? – xro7

+0

拋出什麼異常? –

+0

你可以發佈錯誤的完整Stactrace,並可能指出它發生在哪一行?這將有很大的幫助 – Gumbo

回答

0

input.nextInt()後,您需要添加一個input.nextLine();比你可以閱讀品位。

  System.out.print("Enter hours:"); 
      hours = input.nextInt(); 
      input.nextLine(); 
      course.setHours(hours); 

爲什麼需要它?看到這個問題:Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

您應該添加一個非常簡單的長度驗證,當你輸入等級:

  String enteredGrade = ""; 
      while(enteredGrade.length()!=1) { 
       System.out.print("Enter grade:"); 
       enteredGrade = input.nextLine(); 
       if(enteredGrade.length()==1) { 
        grade = enteredGrade.charAt(0); 
       } else { 
        System.out.println("Type only one character!"); 
       } 
      } 

等完整的主類代碼:

import java.util.Scanner; 

/** 
* Created by dkis on 2016.10.22.. 
*/ 
public class App { 
    public static void main(String[] args) { 

     final int NUM_STUDENTS = 10; 
     final int NUM_COURSES = 5; 

     Student[] students = new Student[NUM_STUDENTS]; 
     //String name; 
     int s;//subscript to display the students 
     int c;//subscript to display courses 
     int stId; 
     int csIndex; 
     String courseId = ""; 
     int hours = 0; 
     char grade = 'z'; 
     CollegeCourse course = new CollegeCourse(courseId,hours, grade);//not sure if I am handling this correctly 

     Scanner input = new Scanner(System.in); 

     for(s = 0; s<NUM_STUDENTS; ++s) 
     { 
      students[s] = new Student(s); 
      System.out.print("Enter ID for student #" + s+1 + ":"); 
      stId = input.nextInt(); 
      input.nextLine(); 
      students[s].setStudentId(stId); 
      for(c=0; c < NUM_COURSES; ++c) 
      { 
       //CollegeCourse course = students[s].getCourse(c); 
       csIndex=c; 
       System.out.print("Enter course ID#" + c+1 + ":"); 
       courseId = input.nextLine(); 
       course.setCourseId(courseId); 
       System.out.print("Enter hours:"); 
       hours = input.nextInt(); 
       input.nextLine(); 
       course.setHours(hours); 

       String enteredGrade = ""; 
       while(enteredGrade.length()!=1) { 
        System.out.print("Enter grade:"); 
        enteredGrade = input.nextLine(); 
        if(enteredGrade.length()==1) { 
         grade = enteredGrade.charAt(0); 
        } else { 
         System.out.println("Type only one character!"); 
        } 
       } 
       course.setGrade(grade); 
       students[s].setCourse(csIndex, course); 
      } 
     } 

     for(s = 0; s<NUM_STUDENTS; ++s) 
     { 
      System.out.print("\nStudent# " + 
        students[s].getStudentId()); 
      for(c=0;c<NUM_COURSES;++c) 
       System.out.print(students[s].getCourse(c) + " "); 
      System.out.println(); 
     } 
    } 
} 
+0

我按照建議修改了我的代碼,並且接受輸入。在第二個循環中添加課程,輸入課程ID後,它將停止並且不會顯示下一行,直到您按Enter鍵。我也希望它能夠將學生列爲1,2 ......但它顯示的是01,11等,對於學生和課程編號是01,11,21等,學生的輸出是可以的,但是課程的輸出結果如下:學生#1CollegeCourse @ 55f96302 CollegeCourse @ 55f96302 – cccstudent

+0

我已經包含了我的主要類的完整代碼 –

+0

糟糕,我在第二個塊中留下了'System.out.println();'將更正它 –