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;
}
}
}
}
}
}
运行结果:
要求完成,美滋滋。
版权声明:本文为LOL_toulan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END