Java将类数组传递给数组

时间:2016-10-22 18:12:23

标签: java arrays class

我是一名学生并正在寻求帮助。这是任务:创建一个CollegeCourse课程。该类包含课程ID的字段(例如,“CIS 210”),学分(例如,3)和字母等级(例如,“A”)。

为每个字段包含get()和set()方法。创建一个包含ID号和五个CollegeCourse对象数组的Student类。为学生ID号创建get()和set()方法。还要创建一个返回Student's CollegeCourses之一的get()方法;该方法采用整数参数并返回该位置(0到4)的CollegeCourse。接下来,创建一个set()方法,用于设置学生大学课程之一的值;该方法有两个参数 - 一个CollegeCourse和一个表示CollegeCourse位置(0到4)的整数。

我在第二个for循环中遇到运行时错误,我试图将数据导入到课程数组中。它要求同一行中的CourseID和Hours,无论我对它做出什么反应,我都会收到错误,它似乎正在尝试同时获取所有数组变量。这是我的代码,包括三个类。如果我已经花费了大量时间研究解决这个问题,那么任何向我发送正确方向的帮助都会受到赞赏。

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 Class

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();
    }   
}

}

1 个答案:

答案 0 :(得分: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();
        }
    }
}