撰寫執行緒類,需要傳入一個引數name,引數可以傳遞 蝸牛 或蛤蟆
引數為蝸牛:
每一秒蝸牛移動速度0~1米
引數為蛤蟆:
每1.5秒蛤蟆移動0~2米
每一次移動都會輸出蝸牛|蛤蟆跑出了多宣告,如果跑到了100米則輸出 蝸牛|蛤蟆跑完了100米
uj5u.com熱心網友回復:
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
public class MyThread extends Thread implements Runnable{
private String name;
private volatile double sum=0;
public MyThread(String name) {
this.name = name;
}
public void run() {
while(true) {
if(name=="flog"&&sum<100) {
double distance = Math.random()*2;
sum+=distance;
System.out.println("蛤蟆走了 "+sum+"米");
try {
TimeUnit.MILLISECONDS.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(sum>=100) {
System.out.println("蛤蟆跑完了100米");
}
}else if(name=="snail"&&sum<100) {
double distance = Math.random();
sum+=distance;
System.out.println("蝸牛走了 "+sum+"米");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(sum>=100) {
System.out.println("蝸牛跑完了100米");
}
}else {
return;
}
}
}
public static void main(String[] args) {
new MyThread("snail").start();
new MyThread("flog").start();
}
}
uj5u.com熱心網友回復:
import java.util.ArrayList;
import java.util.List;
public class RaceGame {
public static void main(String[] args) {
GamePlayer gamePlayer = new GamePlayer();
gamePlayer.addAnimal(new Animal("蝸牛", 1.00));
gamePlayer.addAnimal(new Animal("蛤蟆", 2.00));
gamePlayer.start();
try {
gamePlayer.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
;
}
}
class GamePlayer extends Thread {
private List<Animal> animalList;
private boolean gameover = false;
public GamePlayer() {
animalList = new ArrayList<Animal>();
}
public void addAnimal(Animal animal) {
this.animalList.add(animal);
}
public void run() {
while (!gameover) {
for (Animal animal : animalList) {
animal.move();
System.out.println(animal.getName() + "跑了" + animal.getDistance() + "米");
if (animal.getDistance() > 100) {
System.out.println(animal.getName() + "獲勝了!!!");
gameover = true;
}
}
}
}
}
class Animal {
public Animal(String name, double maxSpeed) {
this.name = name;
this.maxSpeed = maxSpeed;
}
private String name;
private double maxSpeed = 0;
private double distance = 0;
public String getName() {
return this.name;
}
public void move() {
double temp = Math.random() * maxSpeed;
distance += temp;
}
public double getDistance() {
return this.distance;
}
}
uj5u.com熱心網友回復:
public class Animal implements Runnable {
private String name;
private int move;
public Animal(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMove() {
if (getName().equals("蝸牛")) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (int) (Math.random() * 3);
} else {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (int) (Math.random() * 4);
}
}
@Override
public void run() {
// TODO Auto-generated method stub
int distance = 0;
while (distance < 100) {
System.out.println(getName() + "已經跑了" + (distance += getMove()));
}
System.out.println(getName() + "已經跑完了100米");
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal a1=new Animal("蝸牛");
Animal a2=new Animal("蛤蟆");
Thread t1=new Thread(a1);
Thread t2=new Thread(a2);
t1.start();
t2.start();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/26020.html
標籤:Java相關
上一篇:不懂就問系列
