/**
*模拟乌龟
*/
public class Tortoise
{
private double speed; //乌龟速度
public Tortoise()
{
}
public Tortoise(double speed)
{
this.speed = speed;
}
public void setSpeed(double speed)
{
this.speed = speed;
}
//模拟乌龟跑的过程
public double run(double length)
{
double time = length / speed;
System.out.println("乌龟跑了" + time + "秒!");
return time;
}
}
/**
*模拟兔子
*/
public class Rabbit
{
private double speed; //兔子跑的速度
private double sleeptime; //兔子打盹时间
public Rabbit()
{
}
public Rabbit(double speed, double sleeptime)
{
this.speed = speed;
this.sleeptime = sleeptime;
}
public void setSpeed(double speed)
{
this.speed = speed;
}
public void setSleeptime(double sleeptime)
{
this.sleeptime = sleeptime;
}
//模拟兔子跑的过程
public double run(double length)
{
double time = length / speed + sleeptime;
System.out.println("兔子跑了" + time + "秒!");
return time;
}
}
/**
*模拟龟兔赛跑的过程
*/
import java.util.Scanner;
public class Match
{
private double length; //赛道长度
private Tortoise tortoise; //乌龟
private Rabbit rabbit; //兔子
private Scanner sc;
public Match()
{
tortoise = new Tortoise();
rabbit = new Rabbit();
Scanner sc = new Scanner(System.in);
System.out.println("万众瞩目的第一届森林运动会之龟兔赛跑即将进行!");
System.out.println("\n请输入:");
System.out.print("兔子的速度:");
rabbit.setSpeed(sc.nextDouble());
System.out.print("兔子打盹时间:");
rabbit.setSleeptime(sc.nextDouble());
System.out.print("乌龟的速度:");
tortoise.setSpeed(sc.nextDouble());
System.out.print("赛道总长度:");
this.length = sc.nextDouble();
}
public void start()
{
double rtime = rabbit.run(length);
double ttime = tortoise.run(length);
if(ttime < rtime)
{
System.out.println("\n比赛结果:乌龟获胜!");
}
else if(ttime > rtime)
{
System.out.println("\n比赛结果:兔子获胜!");
}
else
{
System.out.print("\n比赛结果:天啊,奇迹出现了它们同时跑到了终点!");
}
}
public static void main(String[] args)
{
Match match = new Match();
match.start();
}
}