0

我对 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 --;
}

这是正在调用的学生构造函数。我添加了一些显示语句,看看我是否能找到出错的地方,但没有运气。我认为这可能与从“静态无效”主方法调用的构造函数有关。

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




}
4

2 回答 2

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,返回要打印的字符串

于 2013-06-23T09:24:26.707 回答
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;
于 2013-06-23T11:11:06.250 回答