数组帮助显示工作

时间:2014-03-24 00:35:52

标签: java

public class Month {

    static String months[];

    public static void main(String[] args) {
        // TODO code application logic here
        months = new String[13];
        months[0] = "null";
        months[1] = "january";
        months[2] = "february";
        months[3] = "march";
        months[4] = "april";
        months[5] = "may";
        months[6] = "june";
        months[7] = "july";
        months[8] = "august";
        months[9] = "september";
        months[10] = "october";
        months[11] = "november";
        months[12] = "december";
        System.out.println("enter number:");
        int a = Integer.parseInt(args[0]);
    System.out.println(a);
    } 
}

我必须获取月份编号的用户输入并输出月份名称。 有人可以告诉我为什么我一直在收到错误

int a = Integer.parseInt(args[0]);

这是错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Month.main(Month.java:22)

由于

2 个答案:

答案 0 :(得分:3)

对于用户输入,您希望使用:

    System.out.println("enter number:");
    Scanner input = new Scanner(System.in);
    int a = input.nextInt();

另请注意,要将import java.util.Scanner;添加到文件顶部,请在package行下面

Args是参数,您必须使用命令行中的程序运行它们,或者将它作为参数添加到IDE的“运行”部分。

答案 1 :(得分:0)

您要求用户输入一个号码,但不要将其用于任何内容。 您将main函数的第一个参数传递给parseInt调用,这会导致您获得错误。