如果 Count有两个饼干,Cookie Monster吃了一个,剩下多少饼干?
-一块饼干。
很好。如果Cookie Monster吃了这一块饼干,Count还剩下什么?
-一只空手,没有饼干。还有一个饥饿的Cookie Monster。
大结局:如果Cookie Monster又吃了一块饼干,伯爵手里有什么?
-负一块饼干!
如果Cookie是字符串,你能写出Cookie Monster吗?该类Count已给出:
<b>package</b> count;
<b>import</b> monster.CookieMonster;
<b>public</b> <b>class</b> Count {
<b>public</b> <b>static</b> <b>void</b> main(String[] args) {
String noCookie = CookieMonster.eat(<font>"cookie"</font><font>);
<b>if</b> (noCookie.isEmpty() && CookieMonster.eat(noCookie).length() < noCookie.length()) {
</font><font><i>// The goal is to reach this line</i></font><font>
System.out.println(</font><font>"Minus one cookie!"</font><font>);
}
}
}
</font>
编辑Cookie Monster,使其正常工作:
<b>package</b> monster;
<b>public</b> <b>class</b> CookieMonster {
<b>public</b> <b>static</b> String eat(String cookie) {
<b>return</b> cookie.substring(0, cookie.length() - 6);
}
}