当前位置: 代码迷 >> J2SE >> 请大家帮小弟我补充一下程序代码
  详细解决方案

请大家帮小弟我补充一下程序代码

热度:139   发布时间:2016-04-24 13:16:11.0
请大家帮我补充一下程序代码

下面的程序代码可以运行,结果是:

第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("数组越界");
}

}
}
}


------解决方案--------------------
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]);
}
}
}
}
}
------解决方案--------------------
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("数组越界"); 

}




[code=Java][/code]
------解决方案--------------------
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]);
}
}
}
楼主,试试我的程序吧,我运行过,没有问题的,与你想要的结果是一样的!
------解决方案--------------------
我也来一段。呵呵。测试只需要将0传入方法即可
Java code
    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 ("数组越界");        }    }