网站建设资讯

NEWS

网站建设资讯

java乌龟赛跑地图代码 龟兔赛跑java项目代码

java模拟龟兔赛跑问题用多线程实现

import java.util.Date;

成都创新互联是一家集网站建设,石首企业网站建设,石首品牌网站建设,网站定制,石首网站建设报价,网络营销,网络优化,石首网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

public class Test extends Thread{ private int tortoise_walk = 0; // 乌龟已跑长度存放变量

private int rabbit_walk = 0; // 兔子已跑长度存放变量

private int finish = 1000; // 终点

private volatile boolean hasWinner = false;// 胜利者诞生 /**

*

* @ClassName: Tortoise_Run

* @Description: TODO(乌龟奔跑线程)

* @author guotingchao

* @date 2012-3-6 上午10:20:45

*

*/

class Tortoise_Run implements Runnable {

@Override

public void run() {

try {

while (!hasWinner) {

if (tortoise_walk % 100 == 0 (tortoise_walk != 0||tortoise_walk=finish)) { //乌龟每100米休息500毫秒

System.out.println("乌龟休息中………………");

Thread.sleep(500);

}

tortoise_walk++;

System.out.println("乌龟已跑"+tortoise_walk+"米");

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

} /**

*

* @ClassName: Rabbit_Run

* @Description: TODO(兔子奔跑线程)

* @date 2012-3-6 上午10:25:10

* @author guotingchao

*/

class Rabbit_Run implements Runnable {

@Override

public void run() {

try {

while (!hasWinner) {

if (rabbit_walk % 20 == 0 (rabbit_walk != 0||rabbit_walk=finish)) { //兔子每20米休息500毫秒

System.out.println("兔子休息中………………");

Thread.sleep(500);

}

rabbit_walk=rabbit_walk+5; //每秒跑5步

System.out.println("兔子已跑"+rabbit_walk+"米");

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

@Override

public void run(){

new Thread(new Rabbit_Run()).start();

new Thread(new Tortoise_Run()).start();

}

/**

* @Title: main

* @Description: TODO(

* 赛程1000米,兔子跑5米,乌龟跑1米,兔子每20米休息500毫秒,乌龟每100米休息500毫秒。谁先到终点就结束程序

* ,并显示获胜方。)

* @param @param args

* @param @throws Exception 设定文件

* @author guotingchao

* @return void 返回类型

* @throws

*/

public static void main(String[] args) throws Exception {

long temp_actionTime=System.currentTimeMillis();

System.out.println("比赛开始:"+new Date(temp_actionTime)+"毫秒");

Test t=new Test();

new Thread(t).start();

while(true){

if(t.tortoise_walk=t.finish||t.rabbit_walk=t.finish){

t.hasWinner=true;

break;

}

}

String winnnerName=t.tortoise_walkt.rabbit_walk?"乌龟":"兔子";

long temp_lastTime=System.currentTimeMillis();

System.out.println(winnnerName+"胜利");

System.out.println("比赛结束:"+new Date(temp_lastTime)+"毫秒");

System.out.println("所耗时间:"+(temp_lastTime-temp_actionTime)+"毫秒");

System.out.println("兔子="+t.rabbit_walk+" 乌龟="+t.tortoise_walk);

}

}

//不知道兔子和乌龟的步长时间是否按每秒。 这里程序只考虑依次递增频率

用JAVA多线程实现龟兔赛跑

程序如下:

/**

*

* GuiTuSaiPao.java

* @author Antonio

* 2009年9月2日20:16:33

* 实现Runnable接口中的run方法

*

*/

public class GuiTuSaiPao implements Runnable {

private String name;

private int length=0;

public GuiTuSaiPao(){}

public GuiTuSaiPao(String name){

this.name=name;

}

public void run(){

while(true){

//每次停500毫秒

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

//向前跑十米

length+=10;

System.out.println(name+"已跑了"+length+"米.");

//到达终点

if(length=100){

System.out.println(name+",已经到达终点!");

//结束赛跑,break

break;

}

}

}

public static void main(String[] args) {

GuiTuSaiPao wugui=new GuiTuSaiPao("乌龟");

GuiTuSaiPao tuzi=new GuiTuSaiPao("兔子");

Thread thread=new Thread(wugui);

Thread thread2=new Thread(tuzi);

//启动线程

thread.start();

thread2.start();

}

}

输出结果:(不一定每次都一样!)

乌龟已跑了10米.

兔子已跑了10米.

兔子已跑了20米.

乌龟已跑了20米.

乌龟已跑了30米.

兔子已跑了30米.

兔子已跑了40米.

乌龟已跑了40米.

兔子已跑了50米.

乌龟已跑了50米.

乌龟已跑了60米.

兔子已跑了60米.

乌龟已跑了70米.

兔子已跑了70米.

乌龟已跑了80米.

兔子已跑了80米.

兔子已跑了90米.

乌龟已跑了90米.

兔子已跑了100米.

兔子,已经到达终点!

乌龟已跑了100米.

乌龟,已经到达终点!

完全看rp,这就是线程!

给分吧,哈哈

用JAVA编程:编写GUI程序,模拟龟兔赛跑游戏

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; //定义一个JFrame窗体作为显示载体 public class TestTortoiseRabbit extends JFrame { private static final long serialVersionUID = 987654321L; static final int MAX_LENGTH = 700; private JButton begin = new JButton("开始"); private JButton exit = new JButton("退出"); private Rabbit rabbit = new Rabbit("兔子"); private Tortoise tortoise = new Tortoise("乌龟"); private JLabel stateOfRabbit = new JLabel(); private JLabel winner; public TestTortoiseRabbit() { super("龟兔赛跑"); setLayout(null); setBounds(200, 150, 800, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); add(rabbit); rabbit.setBounds(20, 100, 50, 20); add(tortoise); tortoise.setBounds(20, 150, 50 ,20); add(stateOfRabbit); stateOfRabbit.setBounds(300, 50, 100 ,20); add(begin);begin.setBounds(20, 200, 80, 30); add(exit);exit.setBounds(100, 200, 80, 30); setVisible(true); begin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new Thread(rabbit).start(); new Thread(tortoise).start(); begin.setVisible(false); stateOfRabbit.setText("兔子跑起来了!"); } }); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String[] args) { new TestTortoiseRabbit(); } class Tortoise extends JLabel implements Runnable { private static final long serialVersionUID = 12345678L; public Tortoise(String name) { super(name); } public Tortoise(Icon icon) { super(icon); } boolean stop; int speed = 2; int x, y; public void run() { x = getLocation().x; y = getLocation().y; while (!stop) { x += speed; setLocation(x, y); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } if (x = MAX_LENGTH) { if (TestTortoiseRabbit.this.winner == null) { TestTortoiseRabbit.this.winner = this; } else { JOptionPane.showMessageDialog(TestTortoiseRabbit.this, TestTortoiseRabbit.this.winner.getText()+ "胜利!"); } stop = true; } } } } class Rabbit extends JLabel implements Runnable { private static final long serialVersionUID = 123456789L; public Rabbit(String name) { super(name); } public Rabbit(Icon icon) { super(icon); } boolean stop; int speed = 5; int x, y; Random r = new Random(); public void run() { x = getLocation().x; y = getLocation().y; while (!stop) { x += speed; setLocation(x, y); try { Thread.sleep(50); if(r.nextInt(100) 90){ if(speed == 0){ speed = 3; stateOfRabbit.setText("兔子跑起来了!"); }else{ speed = 0; stateOfRabbit.setText("兔子在睡觉!"); } } } catch (InterruptedException e) { e.printStackTrace(); } if (x = MAX_LENGTH) { stateOfRabbit.setText("兔子到终点了!"); if (TestTortoiseRabbit.this.winner == null) { TestTortoiseRabbit.this.winner = this; } else { JOptionPane.showMessageDialog(TestTortoiseRabbit.this, TestTortoiseRabbit.this.winner.getText()+ "胜利!"); } stop = true; } } } } }

已知龟兔起点相差1000米,乌龟速度10,兔子速度20,求多久能相遇,用JAVA代码怎么写?

LZ题目给的不是很准确。。跑道是否为环形跑道?

要用JAVA 写出来,首先就要去分析这道数学题中的逻辑问题

1:若为直线跑道,要有相遇 必然是乌龟在前,兔子在后。。定义为追击问题。。

求出速度差 v = 20 -10 = 10

追击路程为 s = 1000

可以得出相遇时间,也就是兔子追上乌龟的时间为 t = 1000/10 = 100(单位题目没有给出。分析应该是min 分钟)

2:若为环形跑道

一圈是多少米,题目没有给出。。故无法算出

分析完数学逻辑,再来写程序代码

public class Test9 {

public static void main(String[] args) {

//定义乌龟速度

int v1 = 10;

//定义兔子速度

int v2 = 20;

//定义整个路程

int sum = 1000;

//求出时间

double t = sum/(v2-v1);

System.out.println(t);

}

}


文章标题:java乌龟赛跑地图代码 龟兔赛跑java项目代码
网页URL:http://njwzjz.com/article/dosdjji.html