在這個問題中,我們將再現經典的龜兔賽跑。程序中使用隨機數生成法來開發一個模擬這一著名事件的應用程序。
比賽場地為70個方格,參賽者從“方格1”開始出發。每個方格代表比賽過程中所經過的一個位置。終點為“方格70”。最先到達或通過“方格70”的參賽者將贏得一桶新鮮的胡蘿卜和萵苣。在比賽過程中可能會經過一段很滑的山路,所以參賽者可能會滑到。
程序中有一個時鐘,每秒滴答一次。隨著每次時鐘滴答,程序應該根據下列規則來調整動物的位置:
image.png
image.png
使用幾個變量來跟蹤動物的位置(即位置號1-70)。在位置1(即起跑線)上啟動每個動物。如果動物在方格1前向左滑動,則將動物移回方格1。 通過產生一個隨機整數i來生成表中的百分比,i的范圍是1〈=i〈=10。對于烏龜而言,當1〈=i〈=5時“快速走”,當6〈=i〈=7時“打滑”,當8〈=i〈=10時“慢速走”。使用類似的方法來移動兔子。比賽開始時打印以下的字符串: 比賽開始了! 程序繼續執行,時鐘每滴答一次(即每循環一次),就打印70號方格位置的一條線,其中烏龜的位置用T表示,兔子的位置用H表示。偶爾,競賽者們會擠到同一個格子上。此時,烏龜會咬兔子,程序要在這位置上打印“OUCH!!!”。所有不是T、H或OUCH!!!(僵局情形)的地方都用空格代替。
父類
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();
}
}