请问为啥会出现数组下标越界呢

新锐码农小L 2016-01-17 06:42:16
public class Test25 {
public static void main(String[] args) {
String s = args[0];
int n = Integer.parseInt(s);
int i = 0;
int[] a = new int[5];
do {
a[i] = n%10;
n /= 10;
++i;
}while(n != 0);
System.out.println("这是一个" + i + "位数,从个位起,各个数字依次为:");
for(int j=0;j<i;j++) {
System.out.println(a[j] + " ");
}
}
}
...全文
411 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
咚咚swwadaw 2016-02-16
  • 打赏
  • 举报
回复
引用 10 楼 u011067688 的回复:

public class Test25 {
    public static void main(String[] args) {
    String s = args[0];
    int size = s.length;
    int n = Integer.parseInt(s);
    int i = 0;
     int[] a = new int[size];
        do {
            a[i] = n%10;
            n /= 10;
            ++i;
        }while(n != 0);
        System.out.println("这是一个" + i + "位数,从个位起,各个数字依次为:");
        for(int j=0;j<i;j++) {
            System.out.println(a[j] + " ");
        }
    }
}
int size = s.length();
咚咚swwadaw 2016-02-16
  • 打赏
  • 举报
回复

public class Test25 {
    public static void main(String[] args) {
    String s = args[0];
    int size = s.length;
    int n = Integer.parseInt(s);
    int i = 0;
     int[] a = new int[size];
        do {
            a[i] = n%10;
            n /= 10;
            ++i;
        }while(n != 0);
        System.out.println("这是一个" + i + "位数,从个位起,各个数字依次为:");
        for(int j=0;j<i;j++) {
            System.out.println(a[j] + " ");
        }
    }
}
Devil-Death 2016-02-16
  • 打赏
  • 举报
回复
public class helloWord { public static void main(String[] args) { String s = args[0]; System.out.println(s); int size = s.length(); int n = Integer.parseInt(s); int i = 0; int[] a = new int[size]; do { a[i] = n%10; n /= 10; ++i; }while(n != 0); System.out.print("这是一个" + i + "位数,从个位起,各个数字依次为:"); for(int j=0;j<i;j++) { System.out.print(a[j] + " "); } } } 控制台输出结果: 1234 这是一个4位数,从个位起,各个数字依次为:4 3 2 1 需要给args传一个参数,用来测试 java Eclipse arguments
weixin_33501608 2016-01-26
  • 打赏
  • 举报
回复
args[0]是你传入args[]中的第一个参数,你要向其中传入参数!亲测可以,你这里写的test25,应该是你看到的演示是传入25,得到的是5,2,0,0,0,
老李家的小二 2016-01-22
  • 打赏
  • 举报
回复
String s = args[0]; int n = Integer.parseInt(s); int m = n; int i = 0; int size = 0; do { m /= 10; size++; }while(m != 0); int[] a = new int[size]; do { a[i] = n%10; n /= 10; ++i; }while(n != 0); System.out.println("这是一个" + i + "位数,从个位起,各个数字依次为:"); for(int j=0;j<i;j++) { System.out.println(a[j] + " "); }
新锐码农小L 2016-01-21
  • 打赏
  • 举报
回复
引用 6 楼 ligyu110 的回复:
public class Test25 { public static void main(String[] args) { String s = args[0]; int n = Integer.parseInt(s); int i = 0; int size = 0; do { n /= 10; size++; }while(n != 0); int[] a = new int[size]; do { a[i] = n%10; n /= 10; ++i; }while(n != 0); System.out.println("这是一个" + i + "位数,从个位起,各个数字依次为:"); for(int j=0;j<i;j++) { System.out.println(a[j] + " "); } } }
还是不行,怎么办,求解决
zhangyw1991 2016-01-19
  • 打赏
  • 举报
回复
限制i的大小,减少do-while循环次数
新锐码农小L 2016-01-19
  • 打赏
  • 举报
回复
请问这里要怎么改呢
老李家的小二 2016-01-19
  • 打赏
  • 举报
回复
public class Test25 { public static void main(String[] args) { String s = args[0]; int n = Integer.parseInt(s); int i = 0; int size = 0; do { n /= 10; size++; }while(n != 0); int[] a = new int[size]; do { a[i] = n%10; n /= 10; ++i; }while(n != 0); System.out.println("这是一个" + i + "位数,从个位起,各个数字依次为:"); for(int j=0;j<i;j++) { System.out.println(a[j] + " "); } } }
fengzhirr123 2016-01-19
  • 打赏
  • 举报
回复
args数组长度为0,可以打印一下args .length
_追逐梦想_ 2016-01-18
  • 打赏
  • 举报
回复
你定义的数组长度只有5,当 索引 ≥ 5时 , a[索引] 就会索引越界了
jtduan 2016-01-17
  • 打赏
  • 举报
回复
int[] a = new int[5]; 给数组分配的空间太小了

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧