线程之龟兔赛跑

题目要求:需求: 乌龟和兔子赛跑总赛程100m, 兔子的速度是10m/s, 乌龟的速度是5m/s.乌龟和兔子都是每跑完10米输出一次结果, 当兔子跑到70米的时候休息2s ,编程模拟比赛过程

public void runTest() {
		Thread t1 = new Thread("小乌龟") {
			public void run() {
				int count = 0;
				while(count<100) {
					try {
                                                //方便在控制台上查看
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					count +=5;
                                        //每10米打印一次
					if (count%10==0) {
						System.out.println("小乌龟跑了"+count+"米");
					}
				}
			}
		};
		
		Thread t2 = new Thread("小兔子") {
			public void run() {
				int count = 0;
				while(count<100) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					count+=10;
					if (count%10==0) {
						System.out.println("小兔子跑了"+count+"米");
					}
                                        //到70米停两秒
					if (count==70) {
						try {
							Thread.sleep(2000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		};
		t1.start();
		t2.start();
	}

输出结果:每次不同

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java线程实现的Swing龟兔赛跑的例子: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Race extends JFrame { private JLabel rabbitLabel; private JLabel turtleLabel; private JButton startButton; public Race() { setTitle("龟兔赛跑"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 200); setLayout(new FlowLayout()); rabbitLabel = new JLabel(new ImageIcon("rabbit.png")); turtleLabel = new JLabel(new ImageIcon("turtle.png")); startButton = new JButton("开始赛跑"); startButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { startRace(); } }); add(rabbitLabel); add(turtleLabel); add(startButton); } private void startRace() { Thread rabbitThread = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i <= 100; i += 10) { rabbitLabel.setLocation(i, rabbitLabel.getY()); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } JOptionPane.showMessageDialog(null, "兔子赢了!"); } }); Thread turtleThread = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i <= 100; i += 5) { turtleLabel.setLocation(i, turtleLabel.getY()); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } JOptionPane.showMessageDialog(null, "乌龟赢了!"); } }); rabbitThread.start(); turtleThread.start(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Race race = new Race(); race.setVisible(true); } }); } } ``` 这个例子使用Java的Swing库来创建一个窗口,其中包含了兔子乌龟的图片以及一个开始赛跑的按钮。当点击开始赛跑按钮,会创建两个线程分别控制兔子乌龟的移动。每个线程都会在一定的间间隔内移动对应的图片,直到达到终点位置。当其中一个线程到达终点,会弹出一个对话框显示谁赢了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值