求编写一个简单的龟兔赛跑的小程序

小 明
2022年度博客之星TOP 3
博客专家认证
2017-07-25 09:07:55
求用数组、字符串包装类、面向对象、继承与多态、抽象类与接口的知识编写一个简单的龟兔赛跑的小程序

大概是这个意思:
写一个动物的抽象类
抽找个方法run
抽象
这个方法可以是操作数组
比如你数组长度为一百
兔子每次操作跳三个
龟跳一个
然后创建两个引用变量 引用兔子和龟
创建两个数组
让对象操作数组,然后打印数组
循环的话就可以看到跑的速度了
...全文
770 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengjianwu1994 2017-07-26
  • 打赏
  • 举报
回复
都是大神
自由自在_Yu 2017-07-26
  • 打赏
  • 举报
回复
线程的可以这样写:
/**
 * 龟兔赛跑---兔子
 * @author 
 * 2017-7-26
 */
public class RabitThread extends Thread{
	private int distance;
	public RabitThread(String name, int distance){
		super(name);
		this.distance = distance;
	}
	
	public void run() {
        long startTime=System.currentTimeMillis();
        int i=0;
        int n=1;
        while (i <= distance) {
            try {
                //每经过100步再休息一小会
                if(i % 1000 == 0){
                	System.out.println("兔子休息等乌龟第" + (n++) + "次,步数:" + i);
                    Thread.sleep(1100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //步长是250cm 兔子是乌龟速度的250倍
            i=i+250;
        }
        long endTime=System.currentTimeMillis();
        System.out.println(getName()+"奔跑"+distance+"cm共耗时:"+(endTime-startTime)+"毫秒");
    }
	
    public void setDistance(int distance){
    	this.distance =  distance;
    }
    
    public int getDistance(){
    	return this.distance;
    }
}
/**
 * 龟兔赛跑---龟
 * @author
 * 2017-7-26
 */
public class TortoiseThread extends Thread{
	private int distance;
	public TortoiseThread(String name, int distance){
		super(name);
		this.distance = distance;
	}
	
	public void run() {
        long startTime=System.currentTimeMillis();
        int i=1;
        while (i <= distance) {
        	 try {
                 //每经过1步再休息一小会,为了模拟两个时间接近,乌龟sleep一下
                 if(i%1==0){
                     Thread.sleep(1);
                 }
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
            //步长是1cm
            i=i+1;
        }
        long endTime=System.currentTimeMillis();
        System.out.println(getName()+"奔跑"+distance+"cm共耗时:"+(endTime-startTime)+"毫秒");
    }
	
    public void setDistance(int distance){
    	this.distance =  distance;
    }
    
    public int getDistance(){
    	return this.distance;
    }
}
/**
 * 龟兔赛跑
 * @author
 * 2017-7-26
 */
public class RunTest {
	public static void main(String[] args) {
		RabitThread rabit = new RabitThread("兔子", 5000);
		TortoiseThread tortoise = new TortoiseThread("乌龟",5000);
		rabit.start();
		tortoise.start();
	}
}
自由自在_Yu 2017-07-26
  • 打赏
  • 举报
回复
这个数组很难实现的,兔子中途要休息的,用线程的sleep方法正好能实现休息等待 数组怎么实现等待着一步呢,走三步退两步吗?
a6727160 2017-07-26
  • 打赏
  • 举报
回复
我就看看,楼下详解
  • 打赏
  • 举报
回复
数组实现太麻烦了,用多线程实现不是更简单
soton_dolphin 2017-07-26
  • 打赏
  • 举报
回复

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Race {
	
	private static ExecutorService service = Executors.newFixedThreadPool(8);
	
	private static int sleep(){
		try{
			Thread.sleep(1000);
		}catch(InterruptedException e){}
		return 1;
	}
	
	private static void tortoise(){
		try{
			Callable<Integer> c = () -> sleep();
			final Collection<Callable<Integer>> r = Arrays.asList(c,c,c,c,c);
			List<Future<Integer>> results = service.invokeAll(r);
			System.out.println("Tortoise won the race!");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private static void hare(){
		try{
			Callable<Integer> c = () -> sleep();
			final Collection<Callable<Integer>> r = Arrays.asList(c,c,c,c,c);
			List<Future<Integer>> results = service.invokeAll(r);
			System.out.println("hare won the race!");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		service.submit(() -> hare());
		service.submit(() -> tortoise());
		Thread.sleep(5000);
		service.shutdown();
	}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧