- /*
- 你代码逻辑就有问题,如果args.length<0,你哪来的args[0]呢?
- 这里必然会抛java.lang.ArrayIndexOutOfBoundsException 异常
- 想求绝对值的话,你是想这么实现吧
- */
- public class if1
- {
- public static void main(String args[])
- {
- if(args.length>0)
- {
- int x=Integer.parseInt(args[0]);
- int y=x;
- if(x<0)//如果x<0,返回0-x即他的绝对值,而不是判断args的长度
- {
- y=0-x;
- }
-
- System.out.println("x绝对值是"+y);
- }
- }
- }
复制代码 |