模拟龟兔赛跑

山东科技大学济南校区课程设计
要求:
能设置比赛时间,马匹的数量等等。
在任意时间段内马匹的速度是随机的。
开始比赛之后以动画显示赛马过程。
提示:使用多线程

package java课程设计;

import java.util.*;
public class mapi{
    public static void main(String args[]){
        Scanner reader=new Scanner(System.in);
        Scanner scanner=new Scanner(System.in);
        System.out.println("            ***********************请设置时间********************");
        int time=reader.nextInt();
        System.out.println("            ***********************请设置马匹数量****************");
        int number=reader.nextInt();
        System.out.println("            ***********************请输入马匹的名字**************");
        String name=scanner.nextLine();
        String regex="[\\s]+";
        String str[]=name.split(regex);
        Match f=null;
        for(int i=0;i<str.length;i++)
        {
            f=new Match(str[i]);
            f.start();
        }   
        while(true)
        {
            System.out.printf("");
            if(f.n==time)
            System.exit(0);
        }

    }
}
class Match extends Thread{
    int length=0;
    String name;
    int n=0;
    public Match(String name)
    {
        this.name=name;
    }
    public void run()
    {
        while(true)
        {
        //length+=10;
        try
        {
                sleep((int)(Math.random()*1000)+100);
        }

        catch(InterruptedException e)
        {
        }
        //System.out.println(name+"跑了"+length+"米");
        //n++;
        length+=10;
        System.out.printf("\n%45s跑了%d米",name,length);
        n++;
        }
    }
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Java程序来模拟龟兔赛跑: ```java import javax.swing.*; import java.awt.*; public class RacingGame extends JFrame { private JLabel lblTortoise; private JLabel lblHare; public RacingGame() { setTitle("龟兔赛跑"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600, 200); setResizable(false); // 创建两个标签来表示乌龟和兔子 lblTortoise = new JLabel(new ImageIcon("tortoise.png")); lblHare = new JLabel(new ImageIcon("hare.png")); // 将标签添加到窗体中 JPanel contentPane = new JPanel(); contentPane.setLayout(null); contentPane.add(lblTortoise); contentPane.add(lblHare); setContentPane(contentPane); // 设置标签的位置和大小 lblTortoise.setBounds(0, 50, 100, 100); lblHare.setBounds(0, 0, 100, 100); setVisible(true); // 开始比赛 startRace(); } private void startRace() { int tortoisePos = 0; int harePos = 0; while (true) { // 计算乌龟和兔子的下一步位置 tortoisePos += (int)(Math.random() * 3) + 1; harePos += (int)(Math.random() * 6) + 1; // 更新标签的位置 lblTortoise.setLocation(tortoisePos, 50); lblHare.setLocation(harePos, 0); // 判断比赛是否结束 if (tortoisePos >= 500) { JOptionPane.showMessageDialog(this, "乌龟赢了!"); break; } else if (harePos >= 500) { JOptionPane.showMessageDialog(this, "兔子赢了!"); break; } // 暂停一下,让用户看清楚每步的移动 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { new RacingGame(); } } ``` 在这个程序中,我们使用了Java Swing库来创建窗体和标签,并使用了一个循环来模拟比赛的过程。每次循环中,我们使用Math.random()方法来计算乌龟和兔子的下一步位置,然后更新标签的位置。最后,我们通过判断乌龟和兔子是否到达终点来结束比赛。整个程序比较简单,适合初学者练手。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值