一个有趣的题目

 

public class Test{

	public static void main(String[] args){
		try {
			
			if(){
				System.out.print("Hello ");
			}else{
				System.out.println("world!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	
}

 以上程序,在不新建类和方法的情况下,在if()里面加代码,使程序编译运行通过,输出Hello world!

 

解法1:

 

public class Test{  
  
    public static void main(String[] args){  
        try {  
              
            if(true)System.out.print("Hello "); if(false){  
                System.out.print("Hello ");  
            }else{  
                System.out.println("world!");  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }   
    }      
}  

 解法2:

 

   public class HelloWorld 
   {
	public static void main(String[] args)
	{
         try {   
               
            if(true){   
                System.out.print("Hello ");   
                	throw new Exception();
            }else{   
                System.out.println("world!");   
            }   
        } catch (Exception e) {   
            
           System.out.println("World") ;
            	
        }    
        }
    }


 

 解法3:

使用匿名类

 

if (new Object() {
				public boolean f() {
					System.out.print("Hello ");
					return false;
				};
			}.f()) {
				System.out.print("Hello ");
			} else {
				System.out.println("world!");
			}

 解法4:

不用自己写方法,不用新建类或者匿名类, 动态脚本执行

 

    if ( 
             new ScriptEngineManager().getEngineByName("JavaScript").eval("print ('Hello ');")==null
                      ) {
				System.out.print("Hello ");
			} else {
				System.out.println("world!");
			}

解法5:

public class Test{  
  
    public static void main(String[] args){  
        try {  
              
            if(new java.io.FileFilter(){public boolean accept(java.io.File file){System.out.print("Hello ");return false;}}.accept(null)){  
                System.out.print("Hello ");  
            }else{  
                System.out.println("world!");  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }   
    }  
      
      
}

  解法6:

c++

#include <iostream>

int main(void){
	if(((std::cout<<"Hello "),false)){
		std::cout<<"Hello "<<std::endl;
	}else{
		std::cout<<"world!"<<std::endl;
	}
	return 0;
}

 解法7:

public class Test{

	public static void main(String[] args){
		try {
			
			if(((System.out.printf("Hello "))==null)){
				System.out.print("Hello ");
			}else{
				System.out.println("world!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	
}

 解法8:

public class Hello {
	public static void main(String[] args) {
		try {
			if (new Hello(){{System.out.print("Hello ");}}==null){
				System.out.print("Hello ");
			} else {
				System.out.println("world!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

  解法9:

用反射

        if(System.out.getClass().getMethod("print",String.class).invoke(System.out, "Hello ")!=null){
            System.out.print("Hello ");  
        } else {  
            System.out.println("world!");  
        }  

解法10:

if(args.length == 0 ? Test.class.getDeclaredMethod("main",args.getClass()).invoke(new Test(), new Object[]{new String[]{"a"}}) != null : true){
                System.out.print("Hello ");
            }else{
                System.out.println("world!");
            }

 解法11:

try {
			if (args == null?true :((Test.class.getMethod("main", new String[1].getClass()).invoke(new Test(), new Object[1])) instanceof Void)
					) {
				System.out.print("Hello ");
			} else {
				System.out.println("world!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

 解法12

public static void main(String args[]){ 

if(args.length == 0){ main(new String[1]); main(new String[2]);} else if(args.length == 1) { 
System.out.print("Hello"); 
} 
else{ 
System.out.print(" world!"); 
}	
}

 解法13:

if (args.length == 0 ? new Object() {
			boolean f() {
				Test.main(new String[2]);
				return false;
			}
		}.f() : true) {
			System.out.print("Hello");
		} else {
			System.out.print(" world!");
		}
	
 

解法14

public class Test{

    public static void main(String[] args){
        try {            
            if(args==null || new Object(){boolean n(){main(null);return false;}}.n()){
                System.out.print("Hello ");
            }else{
                System.out.println("world!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
}

 解法15

	public static void main(String[] args){  
		try { 
			if(new Callable<Boolean>() {
				public Boolean call() {
					System.out.print("Hello ");  
					return false;
				}
			}.call()){  
				System.out.print("Hello ");  
			}else{  
				System.out.println("world!");  
            }  
         } catch (Exception e) {  
            e.printStackTrace();  
        }   
	}  

 解法16:

外层 args 的长度为0 

args.length == 0 ? Test.class.getDeclaredMethod("main",args.getClass()).invoke(new Test(), new Object[]{new String[]{"a"}}) != null : true 

Test.class.getDeclaredMethod("main",args.getClass()).invoke(new Test(), new Object[]{new String[]{"a"}}) != null 为false 

所以此时应该打出"world!" 

在此之前 判定 
Test.class.getDeclaredMethod("main",args.getClass()).invoke(new Test(), new Object[]{new String[]{"a"}}) != null 为false  的过程中 
invoke的调用参数 args ="a" 长度是1 

所以args.length == 0的判断结果为true 

此时打印出"Hello " 

合起来就是正确答案

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值