我创建了一个程序来输入学生的详细信息,例如姓名和卷号。卷号必须唯一,用户不能为同一卷号添加多个记录。当调用main函数时,我创建了3个类,即学生类,具有ArrayList的stuMaster类和主类。
以下是主要课程:

public static void main(String[] args) {

    boolean exit = false;
    Scanner input = new Scanner(System.in);

    do {
        System.out.println("Enter any option from below:");
        System.out.println("1 Enter new student");
        System.out.println("2 View all students");
        System.out.println("3 Exit");

        switch (input.nextInt()) {
        case 1:
            System.out.println("Enter the details of student: ");
            StuMaster stm = new StuMaster();
            stm.addStudentDetails();
            break;

        case 2:
            System.out.println("All the records are: ");
            StuMaster stm1 = new StuMaster();
            stm1.displayAllStudents();

        case 3:
            exit = true;
            System.exit(0);

        default:
            System.out.println("Invalid entry!");
        }

    } while (!exit);
}


学生班:

public class Student {

private int rollNumber, marks;
public name;

public Student() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter roll number: ");
    rollNumber = input.nextInt();
    System.out.print("Enter name: ");
    name= input.next();
    System.out.println("Enter marks: ");
    marks = input.nextInt();
}

public String toString() {
    return "Name: " + name + "Roll no.: " + rollNumber + " ,Marks :" + marks;
}

public int getRollNo() {
    return this.rollNumber;
}


StuMaster类,其中调用List并定义了添加学生的方法:

public class StuMaster {

static List<Student> stu = new ArrayList<Student>();
Student stuObj = new Student();

public List<Student> getStudents() {
    return stu;
}

public void addStudentDetails() {

    stu.add(stuObj);
    System.out.println("Student record added! Following are the details that you added: \n" + stuObj);
}

public void displayAllStudents() {
    System.out.println("All the students in the system: ");
    for (Student s1 : stu) {
        System.out.println(s1);
    }
}


我是java的新手,无法理解如何在StuMaster方法的addStudentDetails类中应用检查。

最佳答案

如果您不想集合中有重复项,则程序需要使用Set而不是List。但是,我将向您展示一个对原始程序进行一些修改的程序,从某些角度来看,我认为它更好:

1)删除每次迭代不必要的StuMaster实例

2)从构造函数中删除学生属性的创建

3)更好的可读性

public class Student {
private String name;
private int rollNumber;
private int mark;

public Student(String name, int rollNumber, int mark) {
    this.name = name;
    this.rollNumber = rollNumber;
    this.mark = mark;
}

//equals and hashcode

@Override
public String toString() {
   //to string override
}
}


您可以使用this教程覆盖哈希码和equals方法。

public class StudentManager {
private Set<Student> students = new HashSet<>();

public void addStudent(Student student) {
    students.add(student);
}

public void displayStudents() {
    for (Student student : students) {
        System.out.println(student);
    }
}
}


这是菜单的public static void main方法:

public static void main(String[] args) {

    boolean exit = false;
    Scanner input = new Scanner(System.in);
    StudentManager studentManager = new StudentManager();

    do {
        System.out.println("Enter any option from below:");
        System.out.println("1 Enter new student");
        System.out.println("2 View all students");
        System.out.println("3 Exit");

        switch (input.nextInt()) {
            case 1:
                System.out.println("Enter the details of student: ");
                Student student = createStudent(input);
                studentManager.addStudent(student);
                break;

            case 2:
                System.out.println("All the records are: ");
                studentManager.displayStudents();
                break;

            case 3:
                exit = true;
                System.exit(0);

            default:
                System.out.println("Invalid entry!");
        }

    } while (!exit);
    }


一种基于输入来创建学生的新方法:

public static Student createStudent(Scanner input) {
    System.out.println("Enter roll number: ");
    int rollNumber = input.nextInt();

    System.out.print("Enter name: ");
    String name = input.next();

    System.out.println("Enter marks: ");
    int marks = input.nextInt();

    return new Student(name, rollNumber, marks);
}


PS:为break添加了一个case 2,否则每次您想显示学生时它都会退出程序。

07-26 09:29