程序员买东西笑话

package murauchi;
import java.util.Scanner;
//一个女人让他的程序员丈夫去商店买东西:你去附近的商店买些面包,如果有鸡蛋的话,买12个回来.
//这个丈夫买了12个面包回来,他的妻子大吃一惊:你为什么买了12个面包?!程序员丈夫回答:因为他们有鸡蛋。

public class Test3 {
    public static void main(String[] args) {
        boolean hasMilk = false;
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.print("是否碰到卖牛奶的 :");
            hasMilk = scanner.nextBoolean();
        } catch (Exception e) {
            System.out.println("输入true或false");
            e.printStackTrace();
        }
        sonBuy(hasMilk);
        System.out.println("+++++++++++++++++++++");
        daughter(hasMilk);
        System.out.println("+++++++++++++++++++++");
        husbandBuy();
        System.out.println("+++++++++++++++++++++");
        wifeBuy();
    }

    private static boolean hasEgg() {
        boolean hasEgg = false;
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.print("是否碰到卖鸡蛋的 :");
            hasEgg = scanner.nextBoolean();
        } catch (Exception e) {
            System.out.println("输入true或false");
            e.printStackTrace();
        }
        return hasEgg;
    }

    private static void husbandBuy() {
        if (hasEgg()) {
            System.out.println("丈夫买12个面包");
        } else {
            System.out.println("丈夫买1个面包");
        }
    }

    private static void wifeBuy() {
        if (hasEgg()) {
            System.out.println("妻子买12个鸡蛋");
            System.out.println("妻子买1个面包");
        } else {
            System.out.println("妻子买1个面包");
        }
    }
    //hasMilk不从参数中传递的话if(hasMilk)中取不到
    private static void sonBuy(boolean hasMilk){
        if (hasMilk){
            System.out.println("儿子买些牛奶");
        }else{
            System.out.println("儿子买快乐水");
        }
    }

    private static void daughter(boolean hasMilk) {
        if (hasMilk){
            System.out.println("女儿买些牛奶");
        }else {
            System.out.println("女儿什么都没买");
        }
    }
}

你可能感兴趣的:(java)