一个关于hello word的思考.

if(){
system.out.print("hello ");
}else{
system.out.print(" world ");
}
今天看到这样一个帖子,说在if()中填写什么能够在控制台打印:hello  world 
咋一看,似乎这是不可能,但是当我们将视野放开阔一点,或许就会有答案了。
其实,我可以把这道题理解成一个脑筋急转弯,那样,分析的路子就有了:
既然要打印"hello  world ",这就必须要同时满足if-else,但是我们知道,这
是不可能的,if和else只可能进入一个,既然是不可能,那要想打印出"hello  world ",
我们就得另辟蹊径,那蹊径在哪儿呢?就在我们要填的if()中。想一想,如果我们打印
的"hello "不是在if块中,还能在哪儿?也只有在if的括号中了,这样问题似乎就有了
解决的方向了,但是找到了方向,路该怎么走呢?很显然在括号中是不能直接写:
system.out.print("hello ")的!这就得我们运用平时积累的知识来解决了!帖子中,
我看到了这样的答案:
1、
if(Boolean.parseBoolean(
(new Object(){
public String toString(){
System.out.print("hello ");
return "false";
}
}).toString()
)){
   System.out.print("hello");
}else{
   System.out.print(" world ");
}

2、
if(System.out.append("hello")==null){
            System.out.println("hello");
        }else{
           System.out.println(" world ");
        }
帖子中除了这两个答案,还有一个更精巧了。我刚刚说了,if和else不可能同时满足,但是你
是否发现,上面的两个答案都是进了else块,那我们有没有可能让程序进入if块呢?简单的分析
一下,如果要进if块,那在if()括号中就应该打印出" world "来,可是这样的话,打印出来的也
不会是"hello world"啊,也应该是"world hello"啊!呵呵…… 看看高手是怎么解决的吧:
3、
if( new Thread(){
{
this.start();
}
@Override
public void run() {
System.out.print(" world");
}
}.isAlive()){
System.out.print("hello");
}else{
System.out.println("world");
}
其实答案远远不止这些。有人说这样写有什么意思呢?我觉得有一句话说的很好:“编程一种艺术,发
挥想象力不是很有意思么 ”。其实这样的思维不仅仅只是在编程中需要运用,我们在生活中,在成长的
道路上,往往会遇到一些看似不可能的事,其实这些事是不是真的不可能呢?或许只是因为我们没有充
分地去思考,没有努力地去寻找可能得答案!








使用可变长度数组设计一个类型,提供如下方法 提示 要统计每个单词出现的次数,由于一个方法不能返回2种类型,我们需要把单词和它的出现次数封装到一个类中 去,所以,可以定义一个类型如下: 由于我们统计的有多个单词,所以,我们上面的 countSize 方法的返回类型就可以设计成 WordBean[],如下: public class PatternDemo { //此方法用来统计 content 中的英文单词个数, 请使用正则表达式来做,单词的正则表达式请自行编写, public int countWords(CharSequence content) { ... } //此方法返回一串固定的字符串,已写好,可以直接用。 public StringBuilder getContent() { //此方法的内容如下: StringBuilder builder = new StringBuilder(); builder.append("Hooray! It's snowing! It's time to make a snowman.James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons.In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels.A moment later, the snowman takes James's hand and goes out.They go up, up, up into the air! They are flying! What a wonderful night!The next morning, James jumps out of bed. He runs to the door.He wants to thank the snowman. But he's gone."); // return builder; } //此方法统计出每个单词[不考虑大小写]出现的次数,数据结构请自行定义,设计如下: public ? countSize(CharSequence content) { //TODO ... } //注:? 处是你需要去思考,该设计什么结构来存放结果 } public class WordBean { //属性 private String word; //单词 private int count; //出次 //TODO 请自行完成构造、getter/setter、toString、等相关方法 } public WordBean[] countSize(CharSequence content) { //TODO ... } 最后写一个调用者类,来测试你的实现,如下: public class UsePatternDemo() { public static void main(String[] args) { //TODO ... }
最新发布
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值