java 程序 生子程序

package woman.ham.vagina;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

/**
 *
 * @author keeny
 *
 */
public class GrowSonFactory {
    public static final String BOY = "boy";
    public static final String GIRL = "girl";

    private static void product(String sex) {
        Man man = Man.newInstance();
        Woman woman = Woman.newInstance();
        Son mySon = null;

        while (true) {
            mySon = join(man, woman);
            sleepOneYear();
            if (mySon.getSex().equals(sex))
                break;
            else
                continue;
        }
        System.out.println("\n恭贺喜得贵子:" + mySon.getSex());
    }

    private static void sleepOneYear() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static Son join(Man man, Woman woman) {
        Son son = new Son();
        int strongestSperm = man.getSperm();
        System.out.println();
        int strongestOvum = woman.getOvum();
        if (strongestSperm == strongestOvum) {
            System.out.println("恭喜你:双胞胎!");
        } else {
            if ((strongestSperm + strongestOvum) % 2 == 0) {
                son.setSex(BOY);
            } else {
                son.setSex(GIRL);
            }
        }
        return son;
    }

    private static String inputSex() {
        Scanner scanner = new Scanner(System.in);
        String sex = null;
        if (scanner.hasNext()) {
            sex = scanner.next();
        }
        if (sex == null) {
            System.out.println("输入不能为空!重新输入:");
            return inputSex();
        }
        if (!sex.equalsIgnoreCase(BOY) && !sex.equalsIgnoreCase(GIRL)) {
            System.out.println("输入性别格式错误(boy/girl):" + sex + "重新输入:");
            return inputSex();
        }
        return sex;
    }

    public static void main(String[] args) {
        System.out.println("请选择想要的性别(boy/girl):");
        String sex = inputSex();
        GrowSonFactory.product(sex);
    }
}

class Man {
    public static DecimalFormat format = new DecimalFormat("0.0000");

    public static Man newInstance() {
        return new Man();
    }

    public int getSperm() {
        TreeSet<Integer> sperms = new TreeSet<Integer>();
        double maxSperm = new Random().nextInt(Integer.MAX_VALUE / 10000);
        for (int i = 1; i < maxSperm + 1; i++) {
            System.out.print("\rIn sex ... "
                    + format.format((i / maxSperm) * 100) + " %\t" + i + "/"
                    + (int) maxSperm);
            sperms.add(new Random().nextInt(i));
        }
        return sperms.last();
    }
}

class Woman {
    public static DecimalFormat format = new DecimalFormat("0.0000");

    public static Woman newInstance() {
        return new Woman();
    }

    public int getOvum() {
        TreeSet<Integer> sperms = new TreeSet<Integer>();
        double maxOvum = new Random().nextInt(Integer.MAX_VALUE / 10000);
        for (int i = 1; i < maxOvum + 1; i++) {
            System.out.print("\rIn sex ... "
                    + format.format((i / maxOvum) * 100) + " %\t" + i + "/"
                    + (int) maxOvum);
            sperms.add(new Random().nextInt(i));
        }
        return sperms.last();
    }
}

class Son {
    private String sex;

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值