博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hello,World
阅读量:7126 次
发布时间:2019-06-28

本文共 1836 字,大约阅读时间需要 6 分钟。

引用自http://www.cnblogs.com/jbelial/archive/2013/05/08/3067471.html#2676127

题目:

1 public class text {2     public static void main(String[] args) {3         if ( 你的代码 )4             System.out.print("hello ");5         else 6             System.out.println("world");7     } 8 }

  能使他打印“hello world” 。

  

  可使用的方法如下:

  (一)使用匿名内部类:

1 public class HelloWorld {2     public static void main(String... a) {3        if (new Object(){
{System.out.print("Hello,");}} == null) {4 System.out.print("Hello,");5 } else {6 System.out.println("World");7 }8 }9 }

  (二)利用PrintStream的append或format等方法(append处改为format):

1 public class HelloWorld {2     public static void main(String... a) {3        if (System.out.append("Hello,") == null) {4            System.out.print("Hello,");5        } else {6            System.out.println("World");7        }8     }9 }

  (三)利用反射调用System.out.print 

public class text {    public static void main(String[] args) throws Exception {        if ( System.out.getClass().getMethod("print",String.class).invoke(System.out, "Hello ")!=null)            System.out.print("hello ");        else             System.out.println("world");    } }

  (四)利用jdk1.5的printf方法 

1 public class HelloWorld {2     public static void main(String... a) throws Exception {3        if (System.out.printf("Hello,") == null) {4            System.out.print("Hello,");5        } else {6            System.out.println("World");7        }8     }9 }
(五)匿名内部类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package 
com.iotest;
 
public 
class 
TestIm {
 
     
    
public 
static 
void 
main(String[] args) {
        
if
(
new 
Thread(){ 
            
                
this
.start(); 
            
            
@Override 
            
public 
void 
run() { 
                
System.out.print(
"world"
);
            
        
}.isAlive()){
            
System.out.print(
"hello "
);
        
}
else
{
            
System.out.println(
"world"
);
        
}
    
}
 
     
}
若转载请注明出处!若有疑问,请回复交流!
你可能感兴趣的文章
swift -- 集合
查看>>
Oracle跟踪文件
查看>>
程序员学炒股(7) 股指期货收盘价对第二天开盘价有影响吗?
查看>>
关于离线缓存webView的新方法NSURLProtocol
查看>>
unittest详解(六) 断言
查看>>
F#与数学(I) – PowerPack中的数字类型
查看>>
Window Phone上的F# - 图形计算器
查看>>
lower_bound()返回值
查看>>
面向对象三大特性
查看>>
C语言调用Python
查看>>
scrapy 的框架的安装
查看>>
微信小程序 --- page.js文件
查看>>
Thinkphp --- 入口文件
查看>>
SQLServer2005创建定时作业任务
查看>>
linux服务器外网内网(双网络)搭建
查看>>
RAMCloud:内存云存储的内存分配机制
查看>>
android selector 用法
查看>>
[LintCode] Longest Increasing Subsequence
查看>>
Java线程案例
查看>>
【计算机视觉】基于Kalman滤波器的进行物体的跟踪
查看>>