Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教一个程序问题 (return)
maoone2003





发贴: 11
积分: 0
于 2004-10-28 14:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
{ 请尽量用准确的文字描述作为标题 }


public class Test
{
public static void main(String[] args)
{
int b=6;
System.out.println("b in main() mehod is:"+b);
System.out.println("return value from var method:"+var());
}

static int var()
{
int b=38;
System.out.println("b in var() mehtod="+b);
for(int i=100;i<200;i++)
return (b+i);
return b;
}
}

该程序的运行结果如下:
b in main() mehod is:6
b in var() mehtod=38
return value from var method:138
请问各位高人,var函数的返回值为什么是138呢,for(int i=100;i<200;i++){return(b+1)}有什么意义,是怎么运行的,为什么有两个return呢?
请各位高手指教,越详细越好,小弟不胜感激!


why edited on 2004-10-28 18:53

作者 Re:请教一个程序问题 (return) [Re:maoone2003]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2004-10-28 18:59 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
阁下在另一版上不是说:晕,这个看不懂还怎么深入学习啊,继续努力吧,呵呵
这句话似乎适用于这个问题喔。Embaressed

b in main() mehod is:6 <-- int b=6; System.out.println("b in main() mehod is:"+b);

b in var() mehtod=38 <-- int b=38; System.out.println("b in var() mehtod="+b);

return value from var method:138 <-- System.out.println("return value from var method:"+var()); <-- var(): int b=38; for(int i=100;i<200;i++) return (b+i); // returned with b=38, i=100;
这个for loop 基本上没有用的。



作者 Re:请教一个程序问题 (return) [Re:maoone2003]
阿猫





发贴: 6
积分: 0
于 2004-10-28 19:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
for(int i=100;i<200;i++){return(b+1)}

是一条循环语句!
意思是:当 i<200 时进入循环执行 return(b+1)
总共要执行100次,每次b都要加1最后一次返回当然是138了



作者 Re:请教一个程序问题 (return) [Re:阿猫]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2004-10-28 21:01 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
阿猫 wrote:
for(int i=100;i<200;i++){return(b+1)}

是一条循环语句!
意思是:当 i<200 时进入循环执行 return(b+1)
总共要执行100次,每次b都要加1最后一次返回当然是138了


请看清楚
for(int i=100;i<200;i++){return(b+i)}
是 i 不是 1。
第一趟 return 即返回,哪会执行100次!



作者 Re:请教一个程序问题 (return) [Re:maoone2003]
maoone2003





发贴: 11
积分: 0
于 2004-10-29 09:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
总版主您老人家教训的是,小弟感激涕零,但是还有两个问题请教阁下
1:既然在这个loop中第一趟就返回,那是不是就不执行return b;语句了,而如果把return b语句去掉后程序就会出错,因为没有返回值,i只是在loop内的一个变量,函数找不到i
2:既然第一趟返回了,那么程序为什么要这么写,用两个return的作用何在,是多此一举还是别有深意?
备注:我肯定版主的解释是正确的,但是小弟有这两个问题不甚理解,还请多多指教,谢谢大哥:)



作者 Re:请教一个程序问题 (return) [Re:maoone2003]
jessen





发贴: 3
积分: 0
于 2004-11-01 15:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
for(int i=100;i<200;i++){return(b+1)}

是一条循环语句!
意思是:当 i<200 时进入循环执行 return(b+1)
总共要执行100次,每次b都要加1最后一次返回当然是138了



作者 Re:请教一个程序问题 (return) [Re:maoone2003]
crapaud





发贴: 1
积分: 0
于 2004-11-02 15:50 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如果是return(b+1)那么答案是39
如果是return(b+i)那么答案是138
说明它只运行一次就跳出循环



作者 Re:请教一个程序问题 (return) [Re:maoone2003]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-11-02 16:22 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
简单点讲:

for(int i=100;i<200;i++)
return (b+i);
句改为:
for(int i=100;i<200;i++)b=b+i;
return b;
就不会是138了。



作者 Re:请教一个程序问题 (return) [Re:maoone2003]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2004-11-03 21:00 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
chengbd posted the following three messages:

有些问题应深入研究,对于这些问题,本人很佩服楼主如何想出来的!

既然定义了要返回一个int,就应该有一个return int。至于为什么编译通不过,以后记住就是了。

48935294(幻影风) 20:49:34
不好说什么,话一样也不能证明你是
算我想多了,呵呵,版猪好

{ Please use our Edit function... }



作者 Re:请教一个程序问题 (return) [Re:maoone2003]
woaizpgf

别笑我笨!



发贴: 20
积分: 0
于 2004-11-04 00:51 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
for(int i=100;i<200;i++)
return (b+i);
如果改成
int k;
for(int i=100;i<200;i++)
{
return (b+i);
k=b+i;
}

我想问:在第一次循环就返回了(b+i),那么循环还继续吗?
就是 k=138 还是237呢?



作者 Re:请教一个程序问题 (return) [Re:woaizpgf]
kavinwang

现实版流氓兔

版主


发贴: 529
积分: 38
于 2004-11-04 09:58 user profilesend a private message to usersend email to kavinwangsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
woaizpgf wrote:
for(int i=100;i<200;i++)
return (b+i);
如果改成
int k;
for(int i=100;i<200;i++)
{
return (b+i);
k=b+i;
}

我想问:在第一次循环就返回了(b+i),那么循环还继续吗?
就是 k=138 还是237呢?


都不知是否该回答你,套用你的话,都返回了还循环个屁呀!



作者 Re:请教一个程序问题 (return) [Re:woaizpgf]
ouyong





发贴: 9
积分: 0
于 2004-11-07 21:40 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
woaizpgf wrote:
for(int i=100;i<200;i++)
return (b+i);
如果改成
int k;
for(int i=100;i<200;i++)
{
return (b+i);
k=b+i;
}

我想问:在第一次循环就返回了(b+i),那么循环还继续吗?
就是 k=138 还是237呢?

k=b+i这句没机会运行了。。。



作者 Re:请教一个程序问题 (return) [Re:maoone2003]
鸡肋男





发贴: 237
积分: 12
于 2004-11-08 09:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
^_^,这估计是哪个老师出的题目考学生的吧,那个循环没用,最后一个return也无用,目的是混淆视听。


作者 Re:请教一个程序问题 (return) [Re:maoone2003]
star_wz





发贴: 2
积分: 0
于 2004-11-09 13:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不知大家回复时有没有编译过,最后一个 return b 去掉后,确实编译不能通过,既然已经有返回值了,为何还必须加入 ruturn b 呢?


作者 Re:请教一个程序问题 (return) [Re:maoone2003]
wuyongjin





发贴: 44
积分: 0
于 2004-11-09 13:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
只运行一次 啊


作者 Re:请教一个程序问题 (return) [Re:star_wz]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2004-11-09 18:53 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
star_wz wrote:
不知大家回复时有没有编译过,最后一个 return b 去掉后,确实编译不能通过,既然已经有返回值了,为何还必须加入 ruturn b 呢?

chengbd 君已寫道:
既然定义了要返回一个int,就应该有一个return int。至于为什么编译通不过,以后记住就是了。


敝人补充如下:
由于有 exit 和 continue 的语法,for loop 裡的 return 句并不保证被执行。
可以说是 compiler 分析不够聪明,或者说 Java specifications 没有要求那么分析吧。




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923