Topic: 请教一个程序问题 (return) |
Print this page |
1.请教一个程序问题 (return) | Copy to clipboard |
Posted by: maoone2003 Posted on: 2004-10-28 14:54 { 请尽量用准确的文字描述作为标题 }
该程序的运行结果如下: 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呢? 请各位高手指教,越详细越好,小弟不胜感激! |
2.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: why Posted on: 2004-10-28 18:59 阁下在另一版上不是说:晕,这个看不懂还怎么深入学习啊,继续努力吧,呵呵 这句话似乎适用于这个问题喔。 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 基本上没有用的。 |
3.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: 阿猫 Posted on: 2004-10-28 19:19 for(int i=100;i<200;i++){return(b+1)} 是一条循环语句! 意思是:当 i<200 时进入循环执行 return(b+1) 总共要执行100次,每次b都要加1最后一次返回当然是138了 |
4.Re:请教一个程序问题 (return) [Re: 阿猫] | Copy to clipboard |
Posted by: why Posted on: 2004-10-28 21:01 阿猫 wrote: 唉 请看清楚 for(int i=100;i<200;i++){return(b+i)} 是 i 不是 1。 第一趟 return 即返回,哪会执行100次! |
5.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: maoone2003 Posted on: 2004-10-29 09:54 总版主您老人家教训的是,小弟感激涕零,但是还有两个问题请教阁下 1:既然在这个loop中第一趟就返回,那是不是就不执行return b;语句了,而如果把return b语句去掉后程序就会出错,因为没有返回值,i只是在loop内的一个变量,函数找不到i 2:既然第一趟返回了,那么程序为什么要这么写,用两个return的作用何在,是多此一举还是别有深意? 备注:我肯定版主的解释是正确的,但是小弟有这两个问题不甚理解,还请多多指教,谢谢大哥:) |
6.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: jessen Posted on: 2004-11-01 15:08 for(int i=100;i<200;i++){return(b+1)} 是一条循环语句! 意思是:当 i<200 时进入循环执行 return(b+1) 总共要执行100次,每次b都要加1最后一次返回当然是138了 |
7.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: crapaud Posted on: 2004-11-02 15:50 如果是return(b+1)那么答案是39 如果是return(b+i)那么答案是138 说明它只运行一次就跳出循环 |
8.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: kavinwang Posted on: 2004-11-02 16:22 简单点讲: 把 for(int i=100;i<200;i++) return (b+i); 句改为: for(int i=100;i<200;i++)b=b+i; return b; 就不会是138了。 |
9.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: why Posted on: 2004-11-03 21:00 chengbd posted the following three messages: 有些问题应深入研究,对于这些问题,本人很佩服楼主如何想出来的! 既然定义了要返回一个int,就应该有一个return int。至于为什么编译通不过,以后记住就是了。 48935294(幻影风) 20:49:34 不好说什么,话一样也不能证明你是 算我想多了,呵呵,版猪好 { Please use our Edit function... } |
10.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: woaizpgf Posted on: 2004-11-04 00:51 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呢? |
11.Re:请教一个程序问题 (return) [Re: woaizpgf] | Copy to clipboard |
Posted by: kavinwang Posted on: 2004-11-04 09:58 woaizpgf wrote: 都不知是否该回答你,套用你的话,都返回了还循环个屁呀! |
12.Re:请教一个程序问题 (return) [Re: woaizpgf] | Copy to clipboard |
Posted by: ouyong Posted on: 2004-11-07 21:40 woaizpgf wrote: k=b+i这句没机会运行了。。。 |
13.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: 鸡肋男 Posted on: 2004-11-08 09:15 ^_^,这估计是哪个老师出的题目考学生的吧,那个循环没用,最后一个return也无用,目的是混淆视听。 |
14.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: star_wz Posted on: 2004-11-09 13:10 不知大家回复时有没有编译过,最后一个 return b 去掉后,确实编译不能通过,既然已经有返回值了,为何还必须加入 ruturn b 呢? |
15.Re:请教一个程序问题 (return) [Re: maoone2003] | Copy to clipboard |
Posted by: wuyongjin Posted on: 2004-11-09 13:22 只运行一次 啊 |
16.Re:请教一个程序问题 (return) [Re: star_wz] | Copy to clipboard |
Posted by: why Posted on: 2004-11-09 18:53 star_wz wrote: chengbd 君已寫道: 既然定义了要返回一个int,就应该有一个return int。至于为什么编译通不过,以后记住就是了。 敝人补充如下: 由于有 exit 和 continue 的语法,for loop 裡的 return 句并不保证被执行。 可以说是 compiler 分析不够聪明,或者说 Java specifications 没有要求那么分析吧。 |
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 |