嗨,我正在学习数组,我们的老师给我们布置了一个额外的作业:“创建一个数组,一年中的每个月(一月、二月……)。我的代码如下所示:
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:28:41
错误出现在三个地方:
在您的程序中添加
args不是空数组(确保用户确实提供了至少1个arg)。以调用程序的方式使用java array2.Array2 5
应输出“May”。
if(args.length == 0) { System.out.println(“错误,必须指定一个参数”);} else { int m= Integer.parseInt( args );if(m<0 || m> 12) {System.out.println(“指定的月份无效”);} else {System.out.println(月m);} }
发布于 2017-02-17 04:19:23
传递参数
正如其他答案所述,在运行此问题时,您可能无法传递参数。
如果使用NetBeans、IntelliJ或Eclipse等IDE,则必须深入研究该工具允许您指定要传递给main方法的参数的位置。
例如,在NetBeans中:
Run。Arguments字段中,键入月份数字,如7。OK保存更改。

测试参数
为了进行调试,您应该将参数数组转储到控制台进行验证。并且您应该测试预期的参数数量。下面是执行这两个任务的示例代码。
public class App {
public 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";
System.out.println ( "DEBUG args: " + Arrays.toString ( args ) );
if ( args.length == 0 ) {
System.out.println ( "No month specified. No arguments passed to 'main' method." );
} else if ( args.length == 1 ) { // Else we have a single argument as expected.
int m = Integer.parseInt ( args[ 0 ] );
System.out.println ( months[ m ] );
} else if ( args.length > 1 ) { // Else we have multiple arguments, but expected only one.
System.out.println ( "ERROR - more than one argument passed to 'main' method." );
} else { // Else impossible. Should not reach this point. Defensive programming.
System.out.println ( "ERROR - Unexpectedly reached IF-ELSE. Should be impossible." );
}
…调试参数: 8、7、9
错误-多个参数传递给'main‘方法。
您可以看到这个code run live at IdeOne.com。不幸的是,这个web应用似乎有一个bug:不能传递输入参数。所以目前对我们的目的来说没有什么用处,但我留下了链接,以防将来修复这个bug。
java.time
虽然我知道您正在做练习来学习Java编程,但您应该意识到,在实际工作中,您不需要这样定义月份。相反,您将使用Month enum,它是java.time类的一部分。枚举是一组静态常量。与conventional enums相比,Enums in Java更强大、更灵活、更有用。
Month month = Month.of ( 5 ); // Months are numbered 1-12 for January-December. So 5 = May.
String output = month.getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH ); // Automatically localize the name of the month.
String output2 = Month.DECEMBER.getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH ); // Refer to a `Month` object by the name of a constant. Here: Month.DECEMBER转储到控制台。
System.out.println ( "month.toString(): " + month );
System.out.println ( "output: " + output );
System.out.println ( "output2: " + output2 );month.toString():五月
输出: mai
output2: décembre
发布于 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
复制相似问题