當前位置:首頁 » 編程軟體 » 烏龜賽跑編程

烏龜賽跑編程

發布時間: 2023-08-10 05:03:04

java編程題龜兔賽跑

class Animal {
public double speed;

public void run(int length) {
System.out.println(length/this.speed);
}
}
class Rabbit extends Animal {
Rabbit(int speed) {
super.speed = speed;
}
@Override
public void run(int length) {
System.out.println("Rabbit time = "+length/this.speed +" seconds");
}

}
class Tortoise extends Animal {
Tortoise(int speed) {
super.speed = speed;
}

@Override
public void run(int length) {
System.out.println("Tortoise time = "+length/this.speed +" seconds");
}
}
public class Match {
public static int length = 100;

private static void begin(Rabbit r,Tortoise t) {
r.run(length);
t.run(length);
}

public static void main(String[] args) {
Rabbit r = new Rabbit(20);
Tortoise t = new Tortoise(5);
begin(r,t);
}
}

Ⅱ JAVA的程序設計,設計一個龜兔賽跑的線程類模擬參與賽跑。

感覺挺有趣的,試著寫了個~

public static void main(String[] arg) {
new wugui().run();
new tuzi().run();
}

static class wugui {
final int su = 4;// 烏龜的速度是每秒4米
public static boolean hasEnd = false;// 是否已經跑到終點
public void run() {
new Thread() {
public void run() {
int distance = 0;
while (distance < 100) {
try {
Thread.sleep(1000);
distance += su;
System.out.println("小烏龜跑了" + distance + "米");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hasEnd = true;
if (tuzi.hasEnd) {
System.out.println("嗚嗚,差一點點就贏了~");
} else {
System.out.println("勝利是屬於有準備的人的,你的自大害了你!-------烏龜贏了");
}
}
}.start();
}
}

static class tuzi {
final int su = 5;// 兔子的速度是每秒5米
public static boolean hasEnd = false;// 是否已經跑到終點
public void run() {
new Thread() {
@Override
public void run() {
int distance = 0;// 跑了多少米
boolean hasXiuXi = false;// 是否休息過
while (distance < 100) {
try {
Thread.sleep(1000);
distance += su;
System.out.println("小兔子跑了" + distance + "米");
if (distance > 50 && !hasXiuXi) {
System.out.println("小兔子累了,決定休息一會兒~");
Thread.sleep((long) (10000 * Math.random()));
System.out.println("小兔子休息夠了,又開始跑了,決一勝負吧!");
hasXiuXi = true;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hasEnd = true;
if (wugui.hasEnd) {
System.out.println("嗚嗚,早知道就不休息了~");
} else {
System.out.println("哇哈哈,你個戰5渣也想贏我~~做夢去吧!!-------兔子贏了");
}
}
}.start();
}
}

Ⅲ 酷酷帶你用scratch製作龜兔賽跑

【概述】

烏龜和兔子在舞台上折返跑,烏龜跑得慢,兔子跑得快。

【知識點】

學會放大和縮小角色學會添加和刪除角色學會切換下一個造型學會調整運動的快慢和造型的切換速度【教學目標】

任意添加1個背景,刪除小貓角色,添加1個烏龜角色和1和兔子角色。

【操作步驟】

任意添加1個背景,如下圖:

在角色庫的「動物」分類下,選中烏龜和兔子角色,點擊「確定」按鈕

拖動角色調整角色的位置,使用舞台區右上角的放大和縮小按鈕來調整角色的大小。如下圖:

選中烏龜,為烏龜添加折返跑的腳本,別忘了調整角色的旋轉模式。然後把【移動10步】改成【移動1步】

寫出兔子折返跑的腳本,注意旋轉模式,並將兔子的移動速度設為10

加入【下一個造型】【等待0.2秒】,如下圖:

兔子的移動變成了一卡一卡的,很不協調,兔子的卡頓是由於將【移動10步】跟【等待0.2】秒放在同一個【重復執行】中導致的,每跑10步都要等一下,肯定不流暢,

把不同功能的腳本分開,寫入到不同的【重復執行】中去。

再新建一個【當綠旗被點擊】和【重復執行】,將【下一個造型】【等待0.2秒】放進去,如下圖

可以看到現在好多了。前邊的烏龜之所以不卡頓,是因為移動速度比較慢,看不出來了

想要學習更多知識,記得關注酷酷哦

Ⅳ 用java模擬龜兔賽跑,能設置比賽距離。在任意時間段內兔子的速度是隨機的,烏龜的速度是恆定的。

package test;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
public class Running {
public static int distance;
private static final CountDownLatch CDL = new CountDownLatch(1);
public static final void start(Animal...animals) {
if (animals != null && animals.length > 0) {
Stream.of(animals).forEach(a -> {
Thread th = new Thread(a);
th.start();
});
// register observers to each animal
for (Animal a: animals) {
for (Animal a2: animals) {
a.addObserver(a2);
}
}
CDL.countDown();
System.out.println("Go!!!!!!");
}
}
public static abstract class Animal extends Observable implements Runnable, Observer {
protected final String name;
protected int speed; // m/s
protected boolean randomSpeed;
private int remaining;
private int speedLimitation;
private Random ran;
private ScheledExecutorService es = Executors.();
protected Animal(final String name, final int speedLimitation) {
this.randomSpeed = true;
this.speedLimitation = speedLimitation;
this.ran = new Random();
this.name = name;
}
protected Animal(final int speed, final String name) {
this.name = name;
this.speed = speed;
}
@Override
public void run() {
try {
CDL.await();
} catch (InterruptedException e) {
}
es.scheleAtFixedRate(() -> {
if (randomSpeed) {
int currentSpeed = ran.nextInt(speedLimitation);
if (currentSpeed > speed) {
System.out.println(name + " 開始加速.");
} else if (speed < currentSpeed) {
System.out.println(name + " 慢了下來.");
} else {
System.out.println(name + " 繼續前進.");
}
speed = currentSpeed;
}
if (remaining == 0) {
remaining = distance - speed;
} else {
remaining -= speed;
}
System.out.println(name + " 距離終點還有" + remaining + "米.");
if (remaining <= speed) {
setChanged();
notifyObservers(name);
}
}, 0, 1, TimeUnit.SECONDS);
}
@Override
public void update(Observable o, Object arg) {
String name = (String) arg;
es.shutdownNow();
if (!this.name.equals(name))
System.out.println(name + " 停止了繼續奔跑.");
else
System.out.println(name + " 贏得了比賽!");
}
}
public static void main(String[] args) {
Running.distance = 100;
Running.start(new Rabbit("小兔兔", 5), new Rabbit("小龜龜", 2));
}
}
class Rabbit extends Running.Animal {
protected Rabbit(String name, int speedLimitation) {
super(name, speedLimitation);
}
}
class Tortoise extends Running.Animal {
protected Tortoise(int speed, String name) {
super(speed, name);
}
}

Ⅳ java 用多線程模擬龜兔賽跑:

public class TortoiseAndHareRace {
public static void main(String[] args) {
Runnable vs=new Race();
Thread hare=new Thread(vs,"Hare");
Thread tortoise =new Thread(vs,"Tortoise");
System.out.println("Ready!GO!");
hare.start();
tortoise.start();
}
}
class Race implements Runnable{
private static final int S=1000;
@Override
public void run() {
if(Thread.currentThread().getName().equals("Hare")){
int sHare=0;
while(sHare<=S){
sHare+=5;
if(sHare%20==0)
try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
}
}else{
int sTortoise=0;
while(sTortoise<=S){
sTortoise++;
if(sTortoise%100==0)
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Ⅵ java模擬龜兔賽跑問題用多線程實現

import java.util.Date;
public class Test extends Thread{ private int tortoise_walk = 0; // 烏龜已跑長度存放變數
private int rabbit_walk = 0; // 兔子已跑長度存放變數
private int finish = 1000; // 終點
private volatile boolean hasWinner = false;// 勝利者誕生 /**
*
* @ClassName: Tortoise_Run
* @Description: TODO(烏龜奔跑線程)
* @author guotingchao
* @date 2012-3-6 上午10:20:45
*
*/
class Tortoise_Run implements Runnable {
@Override
public void run() {
try {
while (!hasWinner) {
if (tortoise_walk % 100 == 0 && (tortoise_walk != 0||tortoise_walk>=finish)) { //烏龜每100米休息500毫秒
System.out.println("烏龜休息中………………");
Thread.sleep(500);
}
tortoise_walk++;
System.out.println("烏龜已跑"+tortoise_walk+"米");
}

} catch (InterruptedException e) {
e.printStackTrace();
}
}
} /**
*
* @ClassName: Rabbit_Run
* @Description: TODO(兔子奔跑線程)
* @date 2012-3-6 上午10:25:10
* @author guotingchao
*/
class Rabbit_Run implements Runnable {
@Override
public void run() {
try {
while (!hasWinner) {
if (rabbit_walk % 20 == 0 && (rabbit_walk != 0||rabbit_walk>=finish)) { //兔子每20米休息500毫秒
System.out.println("兔子休息中………………");
Thread.sleep(500);
}
rabbit_walk=rabbit_walk+5; //每秒跑5步
System.out.println("兔子已跑"+rabbit_walk+"米");
}

} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run(){
new Thread(new Rabbit_Run()).start();
new Thread(new Tortoise_Run()).start();
}
/**
* @Title: main
* @Description: TODO(
* 賽程1000米,兔子跑5米,烏龜跑1米,兔子每20米休息500毫秒,烏龜每100米休息500毫秒。誰先到終點就結束程序
* ,並顯示獲勝方。)
* @param @param args
* @param @throws Exception 設定文件
* @author guotingchao
* @return void 返回類型
* @throws
*/
public static void main(String[] args) throws Exception {
long temp_actionTime=System.currentTimeMillis();
System.out.println("比賽開始:"+new Date(temp_actionTime)+"毫秒");
Test t=new Test();
new Thread(t).start();
while(true){
if(t.tortoise_walk>=t.finish||t.rabbit_walk>=t.finish){
t.hasWinner=true;
break;
}
}
String winnnerName=t.tortoise_walk>t.rabbit_walk?"烏龜":"兔子";
long temp_lastTime=System.currentTimeMillis();
System.out.println(winnnerName+"勝利");
System.out.println("比賽結束:"+new Date(temp_lastTime)+"毫秒");
System.out.println("所耗時間:"+(temp_lastTime-temp_actionTime)+"毫秒");
System.out.println("兔子="+t.rabbit_walk+" 烏龜="+t.tortoise_walk);
}
}
//不知道兔子和烏龜的步長時間是否按每秒。 這里程序只考慮依次遞增頻率

熱點內容
人臉驗證演算法 發布:2025-02-05 03:54:45 瀏覽:358
解壓驛站 發布:2025-02-05 03:54:44 瀏覽:598
php系統下載 發布:2025-02-05 03:54:43 瀏覽:143
android相機圖片 發布:2025-02-05 03:54:09 瀏覽:842
php小票列印機 發布:2025-02-05 03:42:00 瀏覽:613
vivo安卓手機如何連接藍牙耳機 發布:2025-02-05 03:40:30 瀏覽:957
youtubeapp緩存 發布:2025-02-05 03:33:10 瀏覽:747
vc遍歷文件夾 發布:2025-02-05 03:33:09 瀏覽:479
怎麼設dns伺服器地址 發布:2025-02-05 03:31:57 瀏覽:758
訪問伺服器文件夾 發布:2025-02-05 03:29:38 瀏覽:38