我的观看记录当然无法正常工作

问题描述 投票:0回答:1

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

我的主班:

import java.util.Scanner;

公共类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;
}

}

询问时出现错误,请进入学生课程:它将始终显示学生ID:无效

java arrays loops if-statement switch-statement
1个回答
0
投票

您忘记了对sc.nextLine()的多次呼叫(以我的计数为5);在sc.nextInt()之后。添加这些可以解决您的问题。

没有使用换行符,您的菜单尝试在下一个int中读取并且由于“ \ n”不是int而感到不满意。

© www.soinside.com 2019 - 2024. All rights reserved.