构造函数未设置数组

时间:2013-06-23 09:20:59

标签: java arrays constructor

我是Java的新手,目前正在夜间课程中学习它。我无法正确设置构造函数,以便将输入的值移动到数组中。我有下面的代码,似乎各个字段正确传递但数组没有填充,我无法弄清楚我做错了什么。我已经通过我的笔记,教科书和这里检查了。我可能只是盯着它看了太久了!

import java.util.Scanner;


public class Main {

static Scanner InOut = new Scanner(System.in);

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    //* Declare and initalize variables. Set maximum number of students to 6.
    String surname = "", forename = "", course = "a", newCourse, tmpStu ="";
    int firstMark = 0, secondMark = 0, thirdMark = 0, finalScore = 0,
        option = 0, studentCount = 0, delNum, index = 0;
    final int MAXSTUDENT = 6;
    Student[] myClass = new Student[MAXSTUDENT];


    //* Set class details for the constructor. 

    while (option != 6) {

        //* Call the menu system to be displayed
        option = menu();

        switch (option) {
            //* Checks the value return by the menu for the selected option
            case 1:
                //* Checks that the maximum number of students has not been exceeded. 
                if (studentCount >= MAXSTUDENT) {
                    System.out.println("Maximum number of student entered. \n\n");
                    menu();
                } else {

                    //* Enter in the student details
                    InOut.nextLine();   /* ensures that the buffer is cleared*/

                    System.out.println("Enter student surname: ");
                    surname = InOut.nextLine();
                    System.out.println("Enter student forename: ");
                    forename = InOut.nextLine();
                    System.out.println("Enter 1st mark: ");
                    firstMark = InOut.nextInt();
                    System.out.println("Enter 2nd mark: ");
                    secondMark = InOut.nextInt();
                    System.out.println("Enter 3rd mark: ");
                    thirdMark = InOut.nextInt();
                    finalScore = 0;
                    //* Passes the entered paramaters to add the student details to the object class.
                    //* Increases the student counter by 1. 
                    myClass[studentCount] = new Student(surname, forename, course, firstMark, secondMark, thirdMark, finalScore);
                    System.out.println(myClass[studentCount]);
                    studentCount++;

                }
                break;

            case 2:
                //* Enter in the student number to be deleted. Number is passed for deletion. 
                //* Reduces the number of student counter. 
                System.out.println("Delete which student?");
                delNum = InOut.nextInt();
                deleteStudent(delNum, studentCount);
                studentCount--;
                break;

            case 3:
                //* Allows for the course name to be updated. 
                InOut.nextLine();   /* ensures that the buffer is cleared*/
                System.out.println("Enter new course: ");
                newCourse = InOut.nextLine();
                Student.setCourse(newCourse);
                break;

            case 4:
                //* Display all details stored in the object class. 
                for (index = 0; index < studentCount; index = index + 1){
                Student.displayDetails(index);
                }
                break;

            case 5:
                //* Input the surname of student and this name is passed to 
                //* display the details for this student only. 
                InOut.nextLine();   /* ensures that the buffer is cleared*/
                System.out.println("Enter surname of student you want to search for: ");
                String searchName = InOut.nextLine();
               //* searchStudent(myClass, searchName, index, studentCount);
                break;

            case 6:
                //* Exit program
                System.out.println("End of program");
                break;
            default:
                System.out.println("Invalid option");
        }

    }

}

public static int menu() {

    //* Display menu options and returns selection. 
    System.out.println("****Student Menu****");
    System.out.println("1. Add new student details");
    System.out.println("2. Delete Student");
    System.out.println("3. Change Course");
    System.out.println("4. Display All Student Details");
    System.out.println("5. Search Student by Name");
    System.out.println("6. Exit");
    System.out.println("\nInput an option:");
    int option = InOut.nextInt();
    return option;
}



public void addStudent(String surname, String forename, String course, int firstMark, int secondMark, int thirdMark, int finalScore)
    //* Accepts passed paramaters of student details and adds them into the object class. 
{
    System.out.println(surname);
    System.out.println(forename);
    System.out.println(course);
    System.out.println(firstMark);
    System.out.println(secondMark);
    System.out.println(thirdMark);
    System.out.println(finalScore);





}

public static void deleteStudent(int delNum, int studentCount)
    //* Accepts student array number to be deleted and displays details of all
    //* students still stored. 
{
 //*       int asize = myClass.length, index;

 //*       for (index = delNum +1; index < asize; index++)
 //*       {
 //*           myClass[index-1] = myClass[index];
 //*       }
 //*       studentCount --;
}

这是被调用的学生构造函数。我添加了一些显示语句,看看我是否能找到它出错的地方,但没有运气。我认为这可能与从“static void”main方法调用的构造函数有关。

import java.util.Scanner;


public class Student {

private String surname, forename;
private int firstMark, secondMark, thirdMark, finalScore, index;
private int stuCount = 0;
private final int NUMMARK = 3;
private static String course = "French";
Scanner InOut = new Scanner(System.in);



public Student(String surname, String forename, String course, int firstMark, int secondMark, int thirdMark, int finalScore)
{
    System.out.println(surname);
    this.surname = surname;
    this.forename = forename;
    this.course = course;
    this.firstMark = firstMark;
    this.secondMark = secondMark;
    this.thirdMark = thirdMark;
    finalScore = ((firstMark + secondMark + thirdMark)/ NUMMARK);
    this.finalScore = finalScore;

}
public static void setCourse (String newCourse) {
    course = newCourse;

}


private String getDetails()
     //* Builds student details and returns them to be displayed. 
{
    String details = "Student Name: " + surname + ", " + forename +"\n";
    details = details + "Course Name: " + course + "\n";
    details = details + "Student's Marks: " + firstMark + ", " + secondMark + ", " + thirdMark + "\n";
    details = details + "Final Mark: " + finalScore + "\n";
    return details;

}

public static void displayDetails(int index)
    //* Displays all student details that have been entered using the student counter
    //* for the number of times to loop. 
{
  //*      if (myClass[index] != null) {
  //*      System.out.println(myClass[index].getDetails());
  //*      }
}




}

2 个答案:

答案 0 :(得分:3)

myClass[0] = new Student(surname, forename, course, firstMark, secondMark, thirdMark, finalScore);
studentCount++;
System.out.println(myClass[0]);

在上一个学生中添加每个新学生,你总是设置数组的第一个位置,它应该是这样的:

myClass[studentCount] = new Student(surname, forename, course, firstMark, secondMark, thirdMark, finalScore);
System.out.println(myClass[studentCount]);//BEFORE the counter increment
studentCount++;

旁注:

此代码:

System.out.println(myClass[studentCount]);

不会打印学生包含的信息。要打印它们,您必须覆盖toString类的Student方法,返回要打印的字符串

答案 1 :(得分:1)

您的代码没有任何严重问题。你只需要编辑它的一部分如下:

首先要查看Student类的getDeatails()方法,你应该将其声明为 保护: 所以新版本如下

protected String getDetails() 

{
    String details = "Student Name: " + surname + ", " + forename + "\n";
    details = details + "Course Name: " + course + "\n";
    details = details + "Student's Marks: " + firstMark + ", " + secondMark + ", " + thirdMark + "\n";
    details = details + "Final Mark: " + finalScore + "\n";
    return details;

}

在第一种情况下你写的是好的。您只需使用适当的方法从数组中检索数据。所以使用以下代码:

//* Passes the entered paramaters to add the student details to the object class.
//* Increases the student counter by 1. 
myClass[studentCount] = new Student(surname, forename, course,   firstMark, secondMark, thirdMark, finalScore);
for(int i = 0;i<=studentCount;i++){
   System.out.println(myClass[i].getDetails());
   System.out.println("-----------------------");
}
studentCount++;
System.out.println("now the student count are: "+studentCount);

以下是案例4:

case 4:
//* Display all details stored in the object class. 
System.out.println("studentcount:"+studentCount);
for (index = 0; index < studentCount; index = index + 1) {
    System.out.println(myClass[index].getDetails());
    System.out.println("---------------------");
}
break;