在微博上看到的一个趣题,有兴趣的可以看看
搜索更多相关的解决方案:
微博
----------------解决方案--------------------------------------------------------
if(true){System.out.println("Hello World") ;}else if(flase)
[ 本帖最后由 虾B写 于 2011-3-26 20:28 编辑 ]
----------------解决方案--------------------------------------------------------
……
[ 本帖最后由 洛云 于 2011-3-27 00:09 编辑 ]
----------------解决方案--------------------------------------------------------
回复 2楼 虾B写
有更有技术含量的回答吗? ----------------解决方案--------------------------------------------------------
考虑递归.
----------------解决方案--------------------------------------------------------
程序代码:
public class Test {
public static void main(String[] args){
if(print() == 2){
System.out.println("Hello ");
}else{
System.out.println("World");
}
}
static Integer print(){
System.out.print("Hello ");
return 1;
}
}
public static void main(String[] args){
if(print() == 2){
System.out.println("Hello ");
}else{
System.out.println("World");
}
}
static Integer print(){
System.out.print("Hello ");
return 1;
}
}
----------------解决方案--------------------------------------------------------
回复 6楼 linjx0123
题目里是说在一对括号里添加代码…… ----------------解决方案--------------------------------------------------------
回复 楼主 洛云
if (new Object() {public int get() {
System.out.print("Hello ");
return 1;
}
}.get() == 2) {
System.out.println("Hello ");
} else {
System.out.println("World");
}
----------------解决方案--------------------------------------------------------
回复 8楼 linjx0123
内部匿名类。总算看到了想要看的答案了。不过返回值其实可以返回false;最后也不用比较是否与2相等了。 ----------------解决方案--------------------------------------------------------