【问题标题】:My view record by course is not working properly我的课程查看记录无法正常工作
【发布时间】:2019-12-05 12:42:39
【问题描述】:

我的程序尚未完成,我需要帮助。我认为我在searchArray(String key) 方法上的编码存在问题。

我的主要课程:

import java.util.Scanner;

public class StudentArray {

    static Scanner sc = new Scanner(System.in);
    static Student[] stud = new Student[100];
    static int count = 0;

    public static void main(String[] args) {

        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();
                    break;
                case 2:
                    viewStud();
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 0:
                    return;
                default:
                    System.out.println("Invalid Option");
            }
        }

    }

    public static void addStud() {
        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[count] = new Student(numID, year, userName, course);
            ++count;

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

    }

    public static void viewStud() {

        int select;

        System.out.println("1. View Record by ID number: " );
        System.out.println("2. View Record by Course: " );
        System.out.println("3. View Record by Course and Year: " );
        System.out.println("4. View All: " );
        select = sc.nextInt();

        while (select < 1 || select > 4){
            System.out.println("Please enter values 1-4 only: ");
            select = sc.nextInt();
        }

        switch (select){

            case 1:
                int view1;
                System.out.println("Please enter Student ID Number: ");
                view1 = sc.nextInt();
                sc.nextLine();
                searchArray(view1);
                break;
            case 2:
                String view2;
                System.out.println("Please enter Student Course: ");
                view2 = sc.nextLine();
                searchArray(view2);
                break;
            case 3:
                break;
            case 4:
                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");
                    }
                }
                break;

        }


    }
    public static void searchArray(int key){
        boolean isExist = false;
        int temp = 0;

        for(int x = 0; x < count; ++x){
            if(key == stud[x].getNumID()){
                temp = x;
                isExist = true;
                break;
            }

        }

        if(isExist){
            System.out.println("1. Student ID: " + stud[temp].getNumID());
            System.out.println("2. Student Name: " + stud[temp].getUserName());
            System.out.println("3. Student Course: " + stud[temp].getCourse());
            System.out.println("4. Student Year: " + stud[temp].getYear() +"\n");
        }
        else
            System.out.println("The Student ID: " +key+ " is invalid");

    }
    public static void searchArray(String key){
        boolean isExist = false;

        for(int x = 0; x < count; ++x){
            if(key.equalsIgnoreCase(stud[x].getCourse())){
                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");
                isExist = true;
            }

        }

        if(isExist == false){
            System.out.println("The Student ID: " +key+ " is invalid");
        }

    }

}

我的学生班级:

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

}

询问时出错请输入学生课程:总是显示学生证:无效

【问题讨论】:

  • 您是否尝试调试您的代码并在收到“错误”时检查变量的值。如果您不这样做,那么这是了解调试器如何工作的好时机。

标签: java arrays loops if-statement switch-statement


【解决方案1】:

你忘记了多次(我数了 5 次)给sc.nextLine() 的电话;在sc.nextInt() 之后。添加这些将解决您的问题。

如果不使用换行符,您的菜单会尝试读取下一个 int 并且不满意,因为 "\n" 不是 int。

【讨论】:

  • 谢谢,我只知道如果之前的输入是字符串 sc.nextLine(),我必须在 sc.nextInt() 之后调用 sc.nextLine()。我不知道这同样适用于案例。
【解决方案2】:

只需将 viewStud() 的 case 2 替换为:

case 2:
            String view2;
            System.out.println("Please enter Student Course: ");
            sc.nextLine();//to remove residual '\n' from sc!!               
            view2 = sc.nextLine();
            searchArray(view2);
            break;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多