在這個問題中啃匿,我們將再現(xiàn)經(jīng)典的龜兔賽跑晃择。程序中使用隨機數(shù)生成法來開發(fā)一個模擬這一著名事件的應(yīng)用程序握童。
比賽場地為70個方格姆怪,參賽者從“方格1”開始出發(fā)。每個方格代表比賽過程中所經(jīng)過的一個位置澡绩。終點為“方格70”稽揭。最先到達(dá)或通過“方格70”的參賽者將贏得一桶新鮮的胡蘿卜和萵苣。在比賽過程中可能會經(jīng)過一段很滑的山路肥卡,所以參賽者可能會滑到溪掀。
程序中有一個時鐘,每秒滴答一次步鉴。隨著每次時鐘滴答揪胃,程序應(yīng)該根據(jù)下列規(guī)則來調(diào)整動物的位置:
使用幾個變量來跟蹤動物的位置(即位置號1-70)。在位置1(即起跑線)上啟動每個動物唠叛。如果動物在方格1前向左滑動只嚣,則將動物移回方格1。 通過產(chǎn)生一個隨機整數(shù)i來生成表中的百分比艺沼,i的范圍是1〈=i〈=10册舞。對于烏龜而言,當(dāng)1〈=i〈=5時“快速走”障般,當(dāng)6〈=i〈=7時“打滑”调鲸,當(dāng)8〈=i〈=10時“慢速走”。使用類似的方法來移動兔子。比賽開始時打印以下的字符串: 比賽開始了测砂! 程序繼續(xù)執(zhí)行飘哨,時鐘每滴答一次(即每循環(huán)一次),就打印70號方格位置的一條線于微,其中烏龜?shù)奈恢糜肨表示,兔子的位置用H表示青自。偶爾株依,競賽者們會擠到同一個格子上。此時延窜,烏龜會咬兔子恋腕,程序要在這位置上打印“OUCH!D嫒稹荠藤!”伙单。所有不是T、H或OUCH9ぁN怯!(僵局情形)的地方都用空格代替牡彻。
父類
package com.TurtleRabbit;
/**
* Created by ttc on 18-1-3.
*/
public abstract class Animal {
int location=0;
public Animal() {}
public abstract void run();
public int getLocation() {
if(location>=69)
{
return 69;
}
if(location<=0)
{
return 0;
}
else
{
return location;
}
}
public void setLocation(int location) {
this.location = location;
}
}
烏龜子類
package com.TurtleRabbit;
/**
* Created by ttc on 18-1-3.
*/
public class Turtle extends Animal{
@Override
public void run()
{
int random=(int)(Math.random()*10)+1;
if(random>=1&&random<=5)
{
location=location+3;
}
if(random>=6&&random<=7)
{
location=location-6;
if(location<=0)
{
location=0;
}
}
if(random>=8&&random<=10)
{
location=location+1;
}
}
}
兔子子類
package com.TurtleRabbit;
/**
* Created by ttc on 18-1-3.
*/
public class Rabbit extends Animal{
@Override
public void run()
{
int random=(int)(Math.random()*10)+1;
if(random>=1&&random<=2)
{
location=location+0;
}
if(random>=3&&random<=4)
{
location=location+9;
}
if(random==5)
{
location=location-12;
if(location<=0)
{
location=0;
}
}
if(random>=6&&random<=8)
{
location=location+1;
}
if(random>=9&&random<=10)
{
location=location-2;
if(location<=0)
{
location=0;
}
}
}
}
跑道與賽跑類
package com.TurtleRabbit;
/**
* Created by ttc on 18-1-3.
*/
public class Run {
private String[] runway=new String[70];
public void runwayInit()
{
for(int i=0;i<runway.length;i++)
{
runway[i]=" ";
}
}
public void start() throws InterruptedException
{
Rabbit rabbit=new Rabbit();
Turtle turtle=new Turtle();
System.out.println("比賽開始扫沼!");
for(int i=0;i>=0;i++)
{
System.out.println("第"+(i+1)+"秒");
if(i>=1)
{
runway[rabbit.getLocation()]=" ";
runway[turtle.getLocation()]=" ";
}
rabbit.run();
runway[rabbit.getLocation()]="H";
turtle.run();
runway[turtle.getLocation()]="T";
if(rabbit.getLocation()==turtle.getLocation())
{
runway[rabbit.getLocation()]="OUCH!";
}
showRunway();
if(rabbit.getLocation()>=runway.length-1&&
turtle.getLocation()>=runway.length-1)
{
System.out.println("平局!");
break;
}
if(rabbit.getLocation()>=runway.length-1)
{
System.out.println("兔子獲勝庄吼!");
break;
}
if(turtle.getLocation()>=runway.length-1)
{
System.out.println("烏龜獲勝缎除!");
break;
}
Thread.sleep(1000);
}
}
public void showRunway()
{
for(int i=0;i<runway.length;i++)
{
System.out.print(runway[i]);
}
System.out.println();
}
}
Main類
package com.TurtleRabbit;
/**
* Created by ttc on 18-1-3.
*/
public class RunTest {
public static void main(String[] args) throws InterruptedException {
Run run=new Run();
run.runwayInit();
run.start();
}
}