Java模拟百米比赛

最近做笔试遇到一个题,要编程模拟百米比赛,现在给它稍微做得完整一点的!

代码如下:

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        // 模拟8个短跑运动员的速度大决战
        Competition competition = new Competition(8);
        try {
            competition.race();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Competition {
    private final int runnerCount;

    Competition(int runnerCount) {
        this.runnerCount = runnerCount;
    }

    public void race() throws InterruptedException {
        Signal.begin = new CountDownLatch(1);
        Signal.end = new CountDownLatch(runnerCount);

        System.out.println("人类速度大决战,百米决赛即将开始!");
        for (int i = 0; i < runnerCount; i++) {
            TimeUnit.MILLISECONDS.sleep(500);
            System.out.print("# ");
        }
        System.out.println();

        // 运动员准备
        for (int i = 1; i <= runnerCount; i++) {
            new Thread(new Runner(i)).start();
        }

        // 裁判下令比赛开始
        new Thread(new Referee()).start();
        Signal.end.await();

        // 比赛结束,对成绩排序
        List<Map.Entry<Integer, Double>> scoreList = new ArrayList<>(Score.scoreMap.entrySet());
        scoreList.sort((e1, e2) -> {
            if (e1.getValue() > e2.getValue()) {
                return 1;
            }
            return -1;
        });

        // 打印成绩
        System.out.println("\n比赛结束!成绩如下:");
        int order = 1;
        for (Map.Entry<Integer, Double> e : scoreList) {
            System.out.printf("第%d名:%d号运动员 : %.3f秒!...起跑反应时间:%.3f秒\n",
                    order, e.getKey(), e.getValue(), Score.startTimeMap.get(e.getKey()));
            order++;
        }
    }
}

class Runner implements Runnable {
    private final int id;

    Runner(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        Random random = new Random();
        try {
            // 运动员等待起跑信号
            Signal.begin.await();
            long start = System.currentTimeMillis();
            // 起跑反应时间在0.1秒到0.3秒之间
            double startTime = (1.0 + random.nextDouble() * 2.0) / 10.0;
            TimeUnit.MILLISECONDS.sleep((long) (startTime * 1000));
            System.out.println(id + "号运动员起跑了...");
            // 假设每个运动员跑完100米的时间在9.58秒到(9.58+3)秒之间
            TimeUnit.MILLISECONDS.sleep((long) (9580 + (random.nextDouble() * 3.0) * 1000));
            long end = System.currentTimeMillis();
            double score = (end - start) / 1000.0;
            System.out.printf("%d号运动员到达终点了...用时%.3f秒!\n", id, score);
            Signal.end.countDown();
            // 记录成绩
            Score.scoreMap.put(id, score);
            Score.startTimeMap.put(id, startTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Referee implements Runnable {
    @Override
    public void run() {
        System.out.println("比赛开始...");
        Signal.begin.countDown();
    }
}

class Signal {
    public static CountDownLatch begin;
    public static CountDownLatch end;
}

class Score {
    public static Map<Integer, Double> scoreMap = new ConcurrentHashMap<>();
    public static Map<Integer, Double> startTimeMap = new ConcurrentHashMap<>();
}

运行效果:

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值