为什么数组值不会在开关情况下延续?



为什么数组值没有保存在我的开关函数中?首先,我按下1,创建了我的花名册值。创建值后,按2会导致ArrayOutOfBound错误。有人能告诉我我做错了什么吗?

请忽略FileNotFoundException。在我修复了我做错的地方之后,需要对这部分进行处理。

public static Scanner keyboard = new Scanner(System.in);
public static int numStudents;
public static String[] nameOfStudent;
public static int[] broncoID;
public static int[] grade;

^这些是我声明的变量

public static void main(String[] args) throws FileNotFoundException {
    startMenus(keyboard);
}
public static void startMenus(Scanner sc) throws FileNotFoundException {
    while (true) {
        System.out.println("(Enter option # and press ENTER)n");
        System.out.println("1. Create new Roster");
        System.out.println("2. Display Current Roster");
        int option = sc.nextInt();
        sc.nextLine();
        switch (option) {
        case 1:
            createRoster();
            break;
        case 2:
            displayRoster();
            break;
        default:
            System.out.println("Unrecognized Option!n");
        }
    }
}
public static void createRoster() throws FileNotFoundException {
    System.out.println("Please list how many students there are.");
    numStudents = keyboard.nextInt();
    nameOfStudent = new String[numStudents];
    broncoID = new int[numStudents];
    grade = new int[numStudents];
    keyboard.nextLine();
    for(int i = 0; i < numStudents; i++){
        System.out.println("Please enter full name of Student.");
        nameOfStudent[i] = keyboard.nextLine();
        System.out.println("Now please enter the BroncoID of Student.");
        broncoID[i] = keyboard.nextInt();
        System.out.println("Now please enter grade of Student (0-100)");
        grade[i] = keyboard.nextInt();
        keyboard.nextLine();
    }
    System.out.println("Roster successfully created!");
    for (int i = 0; i < numStudents ; i++){
        System.out.printf("Name: %9st Bronco ID: %5d t Grade: %2d n", nameOfStudent[i], broncoID[i], grade[i]);
    }
}
public static void displayRoster(){
    System.out.printf("Name: %9st Bronco ID: %5d t Grade: %2d n", nameOfStudent[1], broncoID[1], grade[1]);

}

第一种情况下的printf有效,但在第二种情况下不起作用。我得到的错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Roster.displayRoster(Roster.java:115)
at Roster.startMenus(Roster.java:36)
at Roster.main(Roster.java:16)

数组总是以索引0开头。您在阵列中添加了一个"名册"。此花名册将获得索引0。在displayRoster()方法中,您希望显示索引1,但数组只有索引0。因此,它将获得ArrayIndexOutOfBoundsException。

代替:

System.out.printf("Name: %9st Bronco ID: %5d t Grade: %2d n", nameOfStudent[1], broncoID[1], grade[1]);

尝试:

System.out.printf("Name: %9st Bronco ID: %5d t Grade: %2d n", nameOfStudent[0], broncoID[0], grade[0]);

您在switch语句中使用了两种情况:

情况1和2

然而,您将createflower的输入作为1,将displayflower 的输入作为4

在switch语句中使用"case 1"one_answers"case 4"。或使用:

System.out.println("1. Create new Roster");
System.out.println("2. Display Current Roster");
  1. 更好地将方法显示Roster修改为:

    public static void displayRoster() {
        System.out.println("Enter the record number to be displayed[1 to numStudents :");
        int i = keyboard.nextInt();
        if(i <= nameOfStudent.length)
            System.out.printf("Name: %9st Bronco ID: %5d t Grade: %2d n", nameOfStudent[i-1], broncoID[i-1], grade[i-1]);
        else
            System.out.println("This is where you are trying to access element out of bound.");
    }
    
  2. 输入option = 4指向代码中的default开关盒,该开关盒应为S.out"Unrecognized Option!"

相关内容

  • 没有找到相关文章

最新更新