下面的程序代码可以运行,结果是:第1次循环:
Hello world!
第2次循环:
您好,世界!
第3次循环:
HELLO WORLD!!
第4次循环:
数组越界
请按任意键继续. . .但是我想把结果改变成像下面这样的结果,不知道这么改,请大家帮忙改一下。谢谢!
第一次循环:
Hello world!
第二次循环:
Hello world!
您好!世界!
第三次循环:
Hello world!
您好!世界!
HELLO WORLD!!
第四次循环:
数组越界...public class TestException 
{
public static void main(String[] args) 
{
int i=0;
String ss[] = {"Hello world!","您好,世界!","HELLO WORLD!!"};
for (; i<4; i++ )
{
try
{
System.out.println("第" + (i+1) + "次循环:");
System.out.println(ss[i]);
}
catch (Exception e)
{
System.out.println("数组越界");
}

}
}
}

解决方案 »

  1.   

    public class TestException {
    public static void main(String[] args) {
    int i = 0;
    String ss[] = { "Hello   world!", "您好,世界!", "HELLO   WORLD!!" };
    for (; i < 4; i++) {
    System.out.println("第" + (i + 1) + "次循环:");
    if(i>=ss.length-1){
    System.out.println("数组越界");
    } else {
    for (int j = 0; j <= i; j++) {
    System.out.println(ss[j]);
    }
    }
    }
    }
    }
      

  2.   

    public   class   TestException   

    public   static   void   main(String[]   args)   

    int   i=0; 
    String   ss[]   =   {"Hello   world!","您好,世界!","HELLO   WORLD!!"}; 
    for   (;   i <4;   i++   ) 
    {
    int j=0;
    System.out.println("第"   +   (i+1)   +   "次循环:"); 
    if(i==ss.length)
    j=3;
    for(;j<i+1;j++) 
    {
    try 

    System.out.println(ss[j]); 

    catch   (Exception   e) 

    System.out.println("数组越界"); 

    } } 


      

  3.   

    1楼!你给的程序(if(i>=ss.length-1))有问题。if(i>=ss.length-1) => if(i>=ss.length)修正后:
    int i = 0;
    String   ss[]   =   {"Hello   world!","您好,世界!","HELLO   WORLD!!"}; 
    for (; i < 4; i++) {
    System.out.println("第"   +   (i+1)   +   "次循环:");
    if (i >= ss.length) {
    System.out.println("数组越界");
    } else {
    for (int j = 0; j <= i; j++) {
    System.out.println(ss[j]);
    }
    }
    }
    楼主,试试我的程序吧,我运行过,没有问题的,与你想要的结果是一样的!
      

  4.   

    2楼,不知你用catch块输出“数组越界”是否欠佳呢?既然已经知道在何处有异常产生了,就没有必要让catch块捕捉程序中的异常的。个人意见。
      

  5.   

    我也来一段。呵呵。测试只需要将0传入方法即可    String[] ss={"Hello   world!","您好,世界!","HELLO   WORLD!!"};
        
        public void testStringArray(int index){
         if(index<ss.length){
         for(int i=0;i<=index;i++){
         System.out.println (ss[i]);
         }
         testStringArray(index+1);
         }else{
         System.out.println ("数组越界");
         }
        }