public class stwich1 {
public static void main(String[] args) {
int weekday=Integer.parseInt(args[0]);
switch(weekday)
{
case 0:System.out.println("0");break;
case 1:System.out.println("1");break;
case 2:System.out.println("3");break;
case 3:System.out.println("3");break;
} }}
为什么程序报错:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at stwich1.main(stwich1.java:6)
求大大们给予解释,小弟刚修JAVA
public static void main(String[] args) {
int weekday=Integer.parseInt(args[0]);
switch(weekday)
{
case 0:System.out.println("0");break;
case 1:System.out.println("1");break;
case 2:System.out.println("3");break;
case 3:System.out.println("3");break;
} }}
为什么程序报错:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at stwich1.main(stwich1.java:6)
求大大们给予解释,小弟刚修JAVA
int weekday=Integer.parseInt(args[0]);
java stwich1 2因为你没有传参数进去,如果是命令行,向上面一样运行!别的IDE的,要配置参数的!
因为args是main方法的参数,默认情况是没有值的,如果在main方法中使用,需要为其赋值解决方案如下运行程序的方法:
1.使用命令行
java stwich1 HelloWorld
注:HelloWorld就是给args[0]传的值
如果需要为args传递多个值,命令行如下
java stwich1 Hello World ……
2.使用IDE,比如Eclipse/MyEclipse
需要配置运行程序的参数,具体配置说明
Run Configuration——>Arguments——>Variables——>Edit Variables——>New(Name……,Value……)
配置好了重新run就ok了/*LZ要加油啊*/
System.out.println(“default”);
break;
在switch结束之前加上上面那段即可 就是默认会做上述处理
这是胡言乱语么……
lz的程序根本走不到这一步,在parseInt或者说args[0]时就挂了,没传参数args是个长度为0的空数组
java stwich1 1
java stwich1 1 2 3
那么args={1,2,3} args[0]=1
要是运行的时候楼主只输入 java stwich1
那么args[0] 是没有意义的 所以这里报错了另外一个隐患是: 要是传入的第一个参数必须是int类型的