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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 桉树 中级黑马   /  2013-6-4 22:55  /  965 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. <p>public class text {</p><p>     public static void main(String[] args) {</p><p>         if ( 你的代码 )</p><p>             System.out.print("hello,");</p><p>         else </p><p>             System.out.println("world");</p><p>     } </p><p> } </p>
复制代码
在“你的代码”中添加代码,是它能打印出 "hello world"  。  
   我知道的有4种比较简单的方法:{:soso_e175:}
    一、使用内部类:
  1. <p>public class HelloWorld {</p><p>     public static void main(String[] args) {</p><p>        if (new Object(){{System.out.print("hello,");}} == null) {</p><p>            System.out.print("hello,");</p><p>        } else {</p><p>            System.out.println("world");</p><p>        }</p><p>     }</p><p> }</p>
复制代码
二、使用 PrintStream 的 append 或 format 等方法(append处改为 format):
  1. <p> public class HelloWorld {</p><p>     public static void main(String[] args) {</p><p>        if (System.out.append("hello,") == null) {</p><p>            System.out.print("hello,");</p><p>        } else {</p><p>            System.out.println("world");</p><p>        }</p><p>     }</p><p> }</p>
复制代码
三、利用反射调用System.out.print
  1. <p>public class text {</p><p>    public static void main(String[] args) throws Exception {</p><p>        if ( System.out.getClass().getMethod("print",String.class).invoke(System.out, "hello,")!=null)</p><p>            System.out.print("hello,");</p><p>        else </p><p>            System.out.println("world");</p><p>    } </p><p>}</p>
复制代码
四、利用jdk 1.5 中的 printf 方法
  1. <p>public class HelloWorld {</p><p>     public static void main(String... a) throws Exception {</p><p>        if (System.out.printf("hello,") == null) {</p><p>            System.out.print("hello,");</p><p>        } else {</p><p>            System.out.println("world");</p><p>        }</p><p>     }</p><p> }</p>
复制代码
还有多线程:
  1. <p>public class text { </p><p>    public static void main(String[] args) {</p><p>        if(new Thread(){  </p><p>            {  </p><p>                this.start();  </p><p>            }   </p><p>            public void run() {  </p><p>                System.out.print("world");</p><p>            }  </p><p>        }.isAlive()){</p><p>            System.out.print("hello ");</p><p>        }else{</p><p>            System.out.println("world");</p><p>        }</p><p>    } </p><p>}</p>
复制代码

评分

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

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马