简体   繁体   English

为什么数组值在切换情况下不保留?

[英]Why aren't the array values carrying over in switch cases?

Why aren't the array values saving in my switch functions? 为什么数组值没有保存在我的开关函数中? First I pressed 1, created my roster values. 首先,我按1,创建我的名册值。 After I created the values, I press 2 it leads to ArrayOutOfBound error. 创建值后,按2会导致ArrayOutOfBound错误。 Can anyone tell me what am I doing wrong? 谁能告诉我我在做什么错?

Please ignore the FileNotFoundExceptions. 请忽略FileNotFoundExceptions。 Need to work on that part after I fix what I'm doing wrong. 解决我做错的事情后,需要在该部分上进行工作。

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

^ These are my declared variables ^这些是我声明的变量

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: %9s\t Bronco ID: %5d \t Grade: %2d \n", nameOfStudent[i], broncoID[i], grade[i]);
    }
}

public static void displayRoster(){
    System.out.printf("Name: %9s\t Bronco ID: %5d \t Grade: %2d \n", nameOfStudent[1], broncoID[1], grade[1]);

} }

The printf in the first case works, but it doesn't work in the second case. 第一种情况下的printf有效,但第二种情况下无效。 Error I get: 错误我得到:

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)

An Array always starts with the index 0. You added one "Roster" to your Array. 数组始终以索引0开头。您向数组添加了一个“ Roster”。 This Roster will get index 0. In your displayRoster() Method you want to show index 1, but your array only has index 0. So it will get the ArrayIndexOutOfBoundsException. 该名册将获得索引0。在displayRoster()方法中,您希望显示索引1,但是您的数组仅具有索引0。因此它将获得ArrayIndexOutOfBoundsException。

Instead of: 代替:

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

Try: 尝试:

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

you are using 2 cases in switch statement: 您在switch语句中使用2种情况:

case 1 and 2 情况1和2

However, you're taking input as 1 for createroster and 4 for displayroster 但是,您将输入作为createroster输入1,将displayroster输入为4

either use "case 1" and "case 4" in switch statement. 在switch语句中使用“案例1”和“案例4”。 or use: 或使用:

System.out.println("1. Create new Roster");
System.out.println("2. Display Current Roster");
  1. Better modify the method displayRoster as : 最好将displayRoster方法修改为:

     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: %9s\\t 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. Input option = 4 directs to the default switch case in your code which should S.out " Unrecognized Option!" 输入option = 4指向代码中的default开关大小写,应为S.out“ Unrecognized Option!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM