为什么交换机盒中的数组值没有结转?

时间:2015-11-25 06:07:01

标签: java arrays global-variables

为什么我的交换机功能中没有保存数组值? 首先我按了1,创造了我的名单值。创建值后,按2导致ArrayOutOfBound错误。谁能告诉我我做错了什么?

请忽略FileNotFoundExceptions。在我解决了我做错的事情之后,需要在这部分工作。

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: %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]);

}

第一种情况下的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)

3 个答案:

答案 0 :(得分:4)

数组始终以索引0开头。您添加了一个&#34; Roster&#34;你的阵列。此列表将获得索引0.在您的displayRoster()方法中,您希望显示索引1,但您的数组只有索引0.因此它将获得ArrayIndexOutOfBoundsException。

而不是:

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

尝试:

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

答案 1 :(得分:0)

你在switch语句中使用了2个案例:

案例1和2

但是,你输入的信息为1为createroster,4为displayroster

在switch语句中使用“case 1”和“case 4”。 或使用:

System.out.println("1. Create new Roster");
System.out.println("2. Display Current Roster");

答案 2 :(得分:0)

  1. 最好将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. 输入option = 4会指向代码中的default切换案例,该案例应该是S.out&#34; Unrecognized Option!"