Java之龟兔赛跑

龟兔赛跑,即两个线程通过随机函数,随机的进行赛跑的距离,在写龟兔赛跑之前,先写一个龟的单独赛跑,兔子的也一样

mian函数:

public class Demo {
    public static void main(String[] args) {
        WuguiThread wt = new WuguiThread("乌龟");
        wt.start();   //线程开始
    }
}

 线程函数:

public class WuguiThread extends Thread {
    public WuguiThread() {      //无参构造
    }

    public WuguiThread(String name) {   //有参构造
        super(name);
    }

    static int distance = 100;
    public int predistance = 0;
    static boolean flag = true;

    public void run() {
        double ran = Math.random();
        String name = Thread.currentThread().getName();
        while (flag) {

            try {
                Thread.sleep(100);    //意为让当前线程休眠0.2秒。
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (name.equals("乌龟")) {
                if (Math.random()< 1) { //此意为每秒就向前跑一米
                    predistance += 1;
                    System.out.println(name + "我跑了:" + predistance + "米");
                    if (predistance == distance) {   
                       System.out.println("******************乌龟赢了******************");
                        flag = false;
                        break;   //退出
                    }
                }
            }
        }
    }
}

输出(部分):

由此看出当乌龟跑到100M时,线程终止,代码停止运行。

龟兔赛跑:

由此推出两个动物赛跑:
main函数:

public class Demo {
    public static void main(String[] args) {
        WuguiandTuziThread wt = new WuguiandTuziThread("乌龟");
        WuguiandTuziThread tt = new WuguiandTuziThread("兔子");
        wt.start();   //乌龟线程开始
        tt.start();   //兔子线程开始
    }
}

龟兔线程函数:

public class WuguiandTuziThread extends Thread {
    public WuguiandTuziThread() {
    }

    public WuguiandTuziThread(String name) {
        super(name);
    }

    static int distance = 100;
    public int predistance = 0;
    static boolean flag = true;

    public void run() {
        double ran = Math.random();
        String name = Thread.currentThread().getName();
        while (flag) {

            try {
                Thread.sleep(100);//意为让当前线程休眠0.2秒。
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (name.equals("乌龟")) {
                if (Math.random()< 1) {
                    predistance += 1;
                    System.out.println(name + "我跑了:" + predistance + "米");
                    if (predistance == distance) {
                        System.out.println("******************乌龟赢了******************");
                        flag = false;
                        break;
                    }
                }
            }
            try {
                sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (name.equals("小兔子")) {
                if (Math.random()< 1) {
                    predistance += 1;
                    System.out.println(name + "我跑了:" + predistance + "米");
                    if (predistance == distance) {
                        System.out.println("******************兔子赢了******************");
                        flag = false;
                        break;
                    }
                }
            }
        }
    }
}

结果:

我们发现这样写其实不对,因为在现实中有一个跑到终点,比赛即结束,而这明显不合标准,此外,这段比赛好像打假赛一样,兔子再慢,也不可能和跑乌龟一样,所以我对此段代码进行了更新:

main函数 :

public class Demo {
    public static void main(String[] args) {
        WuguiandTuziThread wt = new WuguiandTuziThread("乌龟");
        WuguiandTuziThread tt = new WuguiandTuziThread("兔子");
        wt.start();   //乌龟线程开始
        tt.start();   //兔子线程开始
    }
}

线程函数:

public class WuguiandTuziThread extends Thread {
    public WuguiandTuziThread() {
    }

    public WuguiandTuziThread(String name) {
        super(name);
    }

    static int distance = 100;
    public int predistance = 0;
    static boolean flag = true;

    public void run() {
        double ran = Math.random();
        String name = Thread.currentThread().getName();
        while (flag) {

            try {
                Thread.sleep(100);//意为让当前线程休眠0.2秒。
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (name.equals("乌龟")) {
                if (Math.random()< 1) {
                    predistance += 1;
                    System.out.println(name + "我跑了:" + predistance + "米");
                    if (predistance == distance) {
                        System.out.println("******************乌龟赢了******************");
                        flag = false;
                        break;
                    }
                }
            }
            try {
                sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (name.equals("小兔子")) {
                if (Math.random()< 0.3) {  //因为兔子跑得快,休息的时间长,所以令他随即率变低,并增加其休眠。
                    predistance += 2;
                    System.out.println(name + "我跑了:" + predistance + "米");
                    if (predistance == distance) {
                        System.out.println("******************兔子赢了******************");
                        flag = false;
                        break;
                    }
                }
            }
        }
    }
}

运行结果:

要求完成,美滋滋。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值