A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 宋玉 中级黑马   /  2014-2-9 14:05  /  1333 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 宋玉 于 2014-2-13 01:16 编辑

class Demo
{
        public static void main(String[] args)
        {
                int x = 1;
                for(show('a'); show('b') && x<3; show('c'))
                {
                        show('d');
                        x++;
                }
        }
        public static boolean show(char ch)
        {
                System.out.println(ch);
                return true;
        }
}
上述程序可以编译运行,请大家帮我解释一下
        public static boolean show(char ch)
        {
                System.out.println(ch);
                return true;
        }
为什么可以这样写,又是输出,返回值类型又是布尔型

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1 淡定

查看全部评分

2 个回复

倒序浏览
//公有的静态的  返回类型为布尔型的  show()方法   接收char 类型的形参ch
public static boolean show(char ch)  
        {
                //输出语句
                System.out.println(ch);
               //因为方法类型为boolean 必须返回布尔型的值   若方法类型为int类型   就返回  整数
                return true;
        }

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1 赞一个!

查看全部评分

回复 使用道具 举报
public static boolean show(char ch)
        {
                System.out.println(ch);//接受的参数ch是char,此句输出ch的值

                return true;//因为show方法的返回值类型为boolean型,所以只能返回true或false
        }
另外在看主函数中的for方法,第二句的判断语句show('b') && x<3都为true时才能继续循环,否则只要有一个为false则循环停止,所以show()的返回值为true,以此来保证for循环不受show()的影响,只受x值得控制
  for(show('a'); show('b') && x<3; show('c'))
                {
                        show('d');
                        x++;
                }

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马