JAVA程序设计(09)-----面对对象设计初级应用 龟兔赛跑

1.乌龟和兔子共有属性和方法 做成父类 避免重复代码

package com.lovo;
/**
 * 类: 动物
 * @author Abe
 * 属性: 名字 步距 总距离 睡觉的日子
 */

public class Animal {
	protected String name;
	protected int step;
	protected int distance;
	protected int sleepDay;
	
	/**
	 * 构造器
	 * @param name
	 */
	public Animal(String name) {
		this.name = name;
	}
	public Animal(){
	}
	
	/**
	 * 方法:获取 名字 行走长度  休息的天数 设置总距离 设置休息天数
	 * @return
	 */
	public String getName() {
		return name;
	}
	public int getStep() {
		return step;
	}
	public int getDistance(){
		return distance;
	}
	public int getSleepDay() {
		return sleepDay;
	}
	public void setDistance(int dis) {
		this.distance += dis;
	}
	public void setSleepDay(int slp) {
		this.sleepDay += slp;
	}
}

2.类:乌龟

package com.lovo;

/**
 * 类 : 乌龟
 * @author Abe 
 * 父类:Animal 
 * 属性:疲劳的日期
 */
public class Tortoise extends Animal {
	private int encourageDay;

	/**
	 * 构造器
	 * @param name
	 */
	public Tortoise(String name) {
		super(name);
	}

	/**
	 * 方法: 走路,每次2~4米
	 */
	public void walk() {
		if (encourageDay == 0 && sleepDay == 0) {
			step = (int) (Math.random() * 3 + 2);
		}
	}

	/**
	 * 方法:30%鼓励 ,之后3天每天可以走双倍的路程,但是之后的第4天只能休息。
	 */
	public void encourage() {
		if (encourageDay == 0 && sleepDay == 0 && Math.random() >= 0.7) {
			step = (int) (Math.random() * 3 + 2) * 2;
			encourageDay = 3;
			sleepDay = 1;
		}
	}

	/**
	 * 方法: 调用、设置 激励天数
	 * 
	 * @return
	 */
	public int getEncourageDay() {
		return encourageDay;
	}

	public void setEncourageDay(int enc) {
		this.encourageDay += enc;
	}

	/**
	 * 方法:每天的行动 1.没激励不睡觉 就看是否发动技能 没发动就普通移动 2.有激励就激励移动 没激励有睡觉就睡觉
	 * @return 每天活动的结果
	 */
	public String everyDay() {
		if (this.getEncourageDay() == 0 && this.getSleepDay() == 0) {
			this.encourage();
			if (this.getEncourageDay() > 0) {
				this.setDistance(this.getStep());
				return this.getName() + "发动了技能《底力》,疯狂的向前跑出了" + this.getStep()	+ "米!!";
			} else {
				this.walk();
				this.setDistance(this.getStep());
				return this.getName() + "跑了" + this.getStep() + "米!";
			}
		} else if (this.getEncourageDay() != 0) {
			this.setDistance(this.getStep());
			this.setEncourageDay(-1);
			return this.getName() + "的《底力》持续中,它疯狂的向前跑出了" + this.getStep()	+ "米!!";
		} else {
			this.setDistance(0);
			this.setSleepDay(-1);
			return this.getName() + "美美的睡了一觉~";
		}
	}
}

3.类:兔子

package com.lovo;
/**
 * 类:兔子
 * @author Abe
 * 父类:Animal
 */

public class Rabbit extends Animal {

	/**
	 * 构造器
	 */
	public Rabbit(String name) {
		super(name);
	}

	/**
	 * 方法:跑步 每次可以跑1~16, 但是一次跑步超过6就要休息1天,超过11就要休息2天
	 */
	public void run() {
		step = (int) (Math.random() * 16 + 1);
		if (step >= 11) {
			sleepDay = 2;
		} else if (step >= 6) {
			sleepDay = 1;
		}
	}
	/**
	 * 方法:没睡觉就跑步
	 */
	public String everyDay(){
		if(this.getSleepDay() == 0 ){
			this.run();
			this.setDistance(this.getStep());
			if (this.getStep() >= 11) {
				return this.getName() + "不可思议的跑出了" + this.getStep()
						+ "米!!!~ 它累的不得不休息两天……";
			}else if(this.getStep() >= 6){
				return this.getName() + "惊人的跑出了" + this.getStep()
						+ "米!!~ 它累的不得不休息一天……";
			}else{
				return this.getName() + "跑了" + this.getStep()
						+ "米!";
			}
		}else{
				this.setDistance(0);
				this.setSleepDay(-1);
				return this.getName() + "懒洋洋的睡了一觉~~";
		}
	}
}

4.开始赛跑吧~~:)

package com.lovo;

/**
 * 开始比赛跑步 先跑完100米获胜
 * @author Abe
 */
public class Running {
	public static void main(String[] args) {
		Tortoise tor = new Tortoise("小乌龟");
		Rabbit rab = new Rabbit("小兔子");
		
		for(int distance = 100 , day = 1 ;tor.getDistance() < 100 && rab.getDistance() < 100 ;day++ ){
			System.out.println("-----------第" + day + "天-----------");

			System.out.println(tor.everyDay());
			System.out.println(rab.everyDay());

			if(tor.getDistance() >= 100 && rab.getDistance() >= 100){
				System.out.println("怎么回事?!" + tor.getName() + rab.getName() + "同时冲过了终点!! 居然是平局");
			}else if(tor.getDistance() >= 100){
				System.out.println(tor.getName() +"取得了胜利!");
			}else if(rab.getDistance() >= 100){
				System.out.println(rab.getName() +"取得了胜利!");
			}else{
				System.out.println(tor.getName() + "一共跑了"+ tor.getDistance() + "米!");
				System.out.println(rab.getName() + "一共跑了"+ rab.getDistance() + "米!");
			}
		}
	}
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下 Java 编写的赛跑模拟。 首先,我们需要定义两个角色——乌子,他们的基本属性包括速度、位置等信息。可以用一个类来表示他们: ```java public class Animal { private String name; // 名称 private int speed; // 速度 private int position; // 位置 // 构造函数 public Animal(String name, int speed) { this.name = name; this.speed = speed; this.position = 0; } // 睡觉 public void sleep() { System.out.println(name + "在睡觉..."); } // 行动 public void move() { int distance = speed; // 行进距离等于速度 position += distance; // 更新位置 System.out.println(name + "跑了" + distance + "米,当前位置:" + position + "米"); } // 获取位置 public int getPosition() { return position; } } ``` 接下来,我们需要模拟比赛的场景。可以使用一个类来表示比赛,包括两个角色和比赛的逻辑: ```java public class Race { private Animal tortoise; // 乌 private Animal hare; // 子 // 构造函数 public Race() { tortoise = new Animal("乌", 5); hare = new Animal("子", 10); } // 开始比赛 public void start() { int totalDistance = 100; // 总距离 while (tortoise.getPosition() < totalDistance && hare.getPosition() < totalDistance) { tortoise.move(); hare.move(); if (hare.getPosition() - tortoise.getPosition() > 10) { tortoise.sleep(); // 子领先10米以上,乌睡觉 } } System.out.println("比赛结束,"); if (hare.getPosition() >= totalDistance && tortoise.getPosition() >= totalDistance) { System.out.println("双方同时到达终点,平局!"); } else if (hare.getPosition() >= totalDistance) { System.out.println("子获胜!"); } else { System.out.println("乌获胜!"); } } } ``` 最后,我们可以在 `main()` 函数中创建比赛对象并开始比赛: ```java public static void main(String[] args) { Race race = new Race(); race.start(); } ``` 这样,我们就成功地用 Java 编写了一个赛跑模拟程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值