【问题标题】:Add, View, Update, and Delete Student Record using Array使用数组添加、查看、更新和删除学生记录
【发布时间】:2020-03-21 22:35:23
【问题描述】:

我的代码需要帮助。我被困在 2(查看学生记录),我们只允许使用数组而不是数组列表或链表。任何建议和帮助都可以。

以下是说明: 编写一个执行以下操作的菜单驱动程序:

1.添加记录 - 可以在列表中添加N条学生记录(ID号、姓名、课程、年份)

2.查看记录 - 可以按ID号,按课程,按课程和年份查看记录 - 或查看全部

3.更新记录 - 可以编辑/修改不包括ID的记录属性

4.删除 - 可以删除学生记录

这是我的代码:

import java.util.Scanner;

public class StudentArray {

public static void main(String[] args){

    getMenu();

}
public static void getMenu( ){
    Student[] stud = new Student[100];

    Scanner sc = new Scanner(System.in);
    int select;
    System.out.println("1. Add Student Record");
    System.out.println("2. View Student Record");
    System.out.println("3. Update Student Record");
    System.out.println("4. Delete Student Record");
    System.out.println("0. Exit");
    select = sc.nextInt();

    switch (select){
        case 1:
            addStud(stud);
            getMenu();
            break;
        case 2:
            viewStud(stud);
            getMenu();
            break;
        case 3:
            break;
        case 4:
            break;
        case 0:
            break;
        default:
    }

}
public static void addStud(Student[] stud){
    Scanner sc = new Scanner(System.in);
     int numID, year;
     String userName, course;

    int addMore;
    int i = 0;
    do{

        System.out.println("1. Enter Student ID: ");
         numID = sc.nextInt();
        sc.nextLine();
        System.out.println("2. Enter Student Name");
         userName = sc.nextLine();
        System.out.println("3. Enter Student Course");
         course = sc.nextLine();
        System.out.println("4. Enter Student Year");
         year = sc.nextInt();
         stud[i] = new Student(numID, year, userName, course);
        ++i;

        System.out.println("To add another Student Record Press 1");
        addMore = sc.nextInt();
    }while (addMore == 1 );

}
public static void viewStud(Student[] stud){
    for(int x = 0; x < stud.length ; ++x){
        System.out.println("1. Student ID: " + stud[x].getNumID());
        System.out.println("2. Student Name: " + stud[x].getUserName());
        System.out.println("3. Student Course: " + stud[x].getCourse());
        System.out.println("4. Student Year: " + stud[x].getYear() + "\n");
    }

}
}

我的学生班级:

public class Student {

private int numID, year;
private String userName, course;

public Student(int numID, int year, String userName, String course) {

    this.numID = numID;
    this.year = year;
    this.userName = userName;
    this.course = course;

}


public int getNumID() {
    return numID;
}

public void setNumID(int numID) {
    this.numID = numID;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getCourse() {
    return course;
}

public void setCourse(String course) {
    this.course = course;
}

}

您好,谢谢 SNJ,

我删除了我的 getMenu 方法并将代码放在 main 中。但是我无法将 Student 数组和 i 设置为静态

import java.util.Scanner;

公共类 StudentArray {

    public static void main(String[] args) {

        static Student[] stud = new Student[100];
        static int i = 0;

        Scanner sc = new Scanner(System.in);
        while (true) {

            int select;
            System.out.println("1. Add Student Record");
            System.out.println("2. View Student Record");
            System.out.println("3. Update Student Record");
            System.out.println("4. Delete Student Record");
            System.out.println("0. Exit");
            select = sc.nextInt();

            switch (select) {
                case 1:
                    addStud(stud);
                    break;
                case 2:
                    viewStud(stud);
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 0:
                    return;
                default:
                    System.out.println("Invalid Option");
            }
        }

    }

    public static void addStud(Student[] stud) {
        Scanner sc = new Scanner(System.in);
        int numID, year;
        String userName, course;

        int addMore;

        do {

            System.out.println("1. Enter Student ID: ");
            numID = sc.nextInt();
            sc.nextLine();
            System.out.println("2. Enter Student Name");
            userName = sc.nextLine();
            System.out.println("3. Enter Student Course");
            course = sc.nextLine();
            System.out.println("4. Enter Student Year");
            year = sc.nextInt();
            stud[i] = new Student(numID, year, userName, course);
            ++i;

            System.out.println("To add another Student Record Press 1");
            addMore = sc.nextInt();
        } while (addMore == 1);

    }

    public static void viewStud(Student[] stud) {

        for (Student element : stud) {
            if (null != element) {
                System.out.println("1. Student ID: " + element.getNumID());
                System.out.println("2. Student Name: " + element.getUserName());
                System.out.println("3. Student Course: " + element.getCourse());
                System.out.println("4. Student Year: " + element.getYear() + "\n");
            }
        }

    }

【问题讨论】:

  • 小话,我不会使用递归(调用你的 getMenu() 方法)。由于 Java 不是尾递归的,我们最终可能会陷入堆栈溢出。如果输入值等于“0”,则使用 while(true) 循环并使用 break 语句。
  • 您面临什么具体问题?
  • 我的 addStudent 方法正在运行,但是当我尝试查看它们时,我在 intelliJ 上收到以下错误:StudentArray.viewStud(StudentArray.java:69) 的线程“main”java.lang.NullPointerException 中的异常在 StudentArray.getMenu(StudentArray.java:28) 在 StudentArray.getMenu(StudentArray.java:25) 在 StudentArray.main(StudentArray.java:7) 我想可能使用 getMenu() 而不是循环?
  • 我觉得这个作业太多了。

标签: java arrays loops oop menu


【解决方案1】:

您在方法调用之间失去了学生价值。如果您将学生数组和 i 设为静态,它将在方法调用之间保留您的值。

import java.util.Scanner;

public class StudentArray {

public static void main(String[] args){

static Student[] stud = new Student[100];
    static int i = 0;

    public static void main(String[] args) {

    getMenu();

    }

    public static void getMenu() {
 Scanner sc = new Scanner(System.in);
    while (true) {

        int select;
        System.out.println("1. Add Student Record");
        System.out.println("2. View Student Record");
        System.out.println("3. Update Student Record");
        System.out.println("4. Delete Student Record");
        System.out.println("0. Exit");
        select = sc.nextInt();

        switch (select) {
        case 1:
        addStud(stud);

        break;
        case 2:
        viewStud(stud);

        break;
        case 3:
        break;
        case 4:
        break;
        case 0:
        return;
        default:
        System.out.println("Invalid Option");
        }
    }

    }

    public static void addStud(Student[] stud) {
    Scanner sc = new Scanner(System.in);
    int numID, year;
    String userName, course;

    int addMore;

    do {

        System.out.println("1. Enter Student ID: ");
        numID = sc.nextInt();
        sc.nextLine();
        System.out.println("2. Enter Student Name");
        userName = sc.nextLine();
        System.out.println("3. Enter Student Course");
        course = sc.nextLine();
        System.out.println("4. Enter Student Year");
        year = sc.nextInt();
        stud[i] = new Student(numID, year, userName, course);
        ++i;

        System.out.println("To add another Student Record Press 1");
        addMore = sc.nextInt();
    } while (addMore == 1);

    }

    public static void viewStud(Student[] stud) {

    for (Student element : stud) {
        if (null != element) {
        System.out.println("1. Student ID: " + element.getNumID());
        System.out.println("2. Student Name: " + element.getUserName());
        System.out.println("3. Student Course: " + element.getCourse());
        System.out.println("4. Student Year: " + element.getYear() + "\n");
        }
    }

    }

【讨论】:

  • 您好,谢谢,我删除了我的 getMenu 方法并将代码放在 main 中。 ,,,
  • 如果答案解决了您的问题。请不要犹豫,接受答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 2011-11-27
  • 1970-01-01
  • 2022-07-01
  • 2010-10-12
相关资源
最近更新 更多