嗨,我正在学习数组,我们的老师给我们布置了一个额外的作业:“创建一个数组,一年中的每个月(一月、二月……)。我的代码如下所示:
package array2;
public class Array2 {
static String months[];
public static void main(String[] args) {
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";
int m = Integer.parseInt( args[0] );
System.out.println( months[ m ] );
}
}但是我得到了一个错误
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at array2.Array2.main(Array2.java:33)
/Users/Mo/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)有人能帮我澄清一下吗?
发布于 2017-02-17 03:24:28

您的代码是正确的。
确保使用参数来运行它。
如果您是在Eclipse中运行:
1. Run Configuration
2. Arguments Tab
3. Enter the number in Program Arguments.https://stackoverflow.com/questions/42282660
复制相似问题