Java 谜题 7:饼干(Cookie)


如果 Count有两个饼干,Cookie Monster吃了一个,剩下多少饼干?
-一块饼干。

很好。如果Cookie Monster吃了这一块饼干,Count还剩下什么?
-一只空手,没有饼干。还有一个饥饿的Cookie Monster。

大结局:如果Cookie Monster又吃了一块饼干,伯爵手里有什么?

-负一块饼干!

如果Cookie是字符串,你能写出Cookie Monster吗?该类Count已给出:

package count;
 
import monster.CookieMonster;
 
public class Count {
    public static void main(String[] args) {
        String noCookie = CookieMonster.eat("cookie");
        if (noCookie.isEmpty() && CookieMonster.eat(noCookie).length() < noCookie.length()) {
           
// The goal is to reach this line
            System.out.println(
"Minus one cookie!");
        }
    }
}

编辑Cookie Monster,使其正常工作:

package monster;
 
public class CookieMonster {
    public static String eat(String cookie) {
        return cookie.substring(0, cookie.length() - 6);
    }
}