您的位置:程序门 -> java -> j2se / 基础类



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


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请大家帮我补充一下程序代码
发表于:2007-12-26 00:10:05 楼主

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

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

}
}
}
发表于:2007-12-26 00:25:141楼 得分:0
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]);
}
}
}
}
}
发表于:2007-12-26 00:44:052楼 得分:0
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("数组越界");  
}  
}

}  
}  
}  
发表于:2007-12-26 00:49:443楼 得分:0
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]);
}
}
}
楼主,试试我的程序吧,我运行过,没有问题的,与你想要的结果是一样的!
发表于:2007-12-26 00:57:404楼 得分:0
2楼,不知你用catch块输出“数组越界”是否欠佳呢?既然已经知道在何处有异常产生了,就没有必要让catch块捕捉程序中的异常的。

个人意见。
发表于:2007-12-26 01:04:485楼 得分:0
我也来一段。呵呵。测试只需要将0传入方法即可
java code
string[] ss={"hello world!","您好,世界!","hello world!!"}; public void teststringarray(int index){ if(index<ss.length){ forint i=0;i<=index;i++){ system.out.println (ss[i]); } teststringarray(index+1); }else{ system.out.println ("数组越界"); } }


快速检索

最新资讯
热门点击