「java龟兔赛跑课程设计」龟兔赛跑教学案例

博主:adminadmin 2022-12-11 07:42:07 75

今天给各位分享java龟兔赛跑课程设计的知识,其中也会对龟兔赛跑教学案例进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java课程设计 求大神解答

思路,其实就是多线程,兔子的线程中间要会sleep下, 速度要用Math.random()这个函数,不会的话,我晚上看看帮你写个

高分急求:基于java模拟龟兔赛跑 课程设计!!!

import java.awt.*;

import javax.swing.*;

import java.net.*;

import java.awt.event.*;

public class Race extends JApplet implements ActionListener{

JButton b=new JButton("开始");

JButton b1=new JButton("复位");

JPanel p=new JPanel();

JLabel message=new JLabel();

JLabel message1=new JLabel();

int rx=0,tx=0;

Image tortoise,rabbit;

int h,w;

Thread t,r;

public void init(){

Container c=getContentPane();

c.setLayout(new GridLayout(3,1));

b.addActionListener(this);

b1.addActionListener(this);

b1.setEnabled(false);

p.add(b1);

p.add(b);

p.add(message);

p.add(message1);

c.add(p);

try{

tortoise=getImage(getDocumentBase(),"gui.jpg");

rabbit=getImage(getDocumentBase(),"tu.jpg");

}catch(Exception e){}

h=getHeight();

w=getWidth();

r=new Rabbit(this);

t=new Tortoise(this);

}

public void paint(Graphics g){

super.paint(g);

g.drawImage(tortoise,tx,60,h/3,h/3,this);

g.drawImage(rabbit,rx,120,h/3,h/3,this);

if(rx=w-h/3rxtx)message.setText("兔子赢得比赛!");

if(tx=w-h/3txrx)message1.setText("乌龟赢得比赛!");

if(tx=w-h/3rx=w-h/3)b1.setEnabled(true);

}

public void start(){ }

public void setM(String m){

message.setText(m);

}

public void setRx(int x){

rx=x;

}

public void setTx(int x){

tx=x;

}

public int getRx(){

return rx;

}

public int getTx(){

return tx;

}

public void actionPerformed(ActionEvent e){

String str=e.getActionCommand();

if(str.equals("开始")){

t.start();

r.start();

b.setEnabled(false);

}

else{

r=new Rabbit(this);

t=new Tortoise(this);

b.setEnabled(true);

b1.setEnabled(false);

rx=0;

tx=0;

repaint();

message.setText("");

message1.setText("");

}

}

}

class Rabbit extends Thread{

Race race;

int rx=0;

boolean wake=true;

public Rabbit(Race r){

race=r;

}

public void run(){

while(true){

int flag=(int)(Math.random()*40);

rx=rx+5;

race.setRx(rx);

race.repaint();

if(race.getRx()=race.getWidth()-race.getHeight()/3)break;

try{

if(flag!=2){

if(!wake){

race.setM("兔子醒了!");

wake=true;

}

Thread.sleep(50);

}

else{

race.setM("兔子睡着了!");

wake=false;

Thread.sleep(3000);

}

}catch(Exception e){}

}

}

}

class Tortoise extends Thread{

Race race;

int tx=0;

public Tortoise(Race r){

race=r;

}

public void run(){

while(true){

tx=tx+2;

race.setTx(tx);

race.repaint();

if(race.getTx()=race.getWidth()-race.getHeight()/3)break;

try{

Thread.sleep(50);

}catch(Exception e){}

}

}

}

这是源代码希望能采纳

JAVA龟兔赛跑问题

先让兔子进入阻塞状态,然后等乌龟跑完终点后唤醒兔子线程就行;下面是各个方法的配套使用(网上copy的,总结的很不错)1. sleep() 方法:sleep() 允许 指定以毫秒为单位的一段时间作为参数,它使得线程在指定的时间

内进入阻塞状态,不能得到CPU 时间,指定的时间一过,线程重新进入可执行状态。

典型地,sleep() 被用在等待某个资源就绪的情形:测试发现条件不满足后,让线程阻塞一段时间后

重新测试,直到条件满足为止。

2. suspend() 和 resume() 方法:两个方法配套使用,suspend()使得线程进入阻塞状态,并且不会

自动恢复,必须其对应的resume() 被调用,才能使得线程重新进入可执行状态。典型地,suspend() 和

resume() 被用在等待另一个线程产生的结果的情形:测试发现结果还没有产生后,让线程阻塞,另一个

线程产生了结果后,调用 resume() 使其恢复。

3. yield() 方法:yield() 使得线程放弃当前分得的 CPU 时间,但是不使线程阻塞,即线程仍处于

可执行状态,随时可能再次分得 CPU 时间。调用 yield() 的效果等价于调度程序认为该线程已执行了足

够的时间从而转到另一个线程。

4. wait() 和 notify() 方法:两个方法配套使用,wait() 使得线程进入阻塞状态,它有两种形式

,一种允许指定以毫秒为单位的一段时间作为参数,另一种没有参数,前者当对应的 notify() 被调用或

者超出指定时间时线程重新进入可执行状态,后者则必须对应的 notify() 被调用。

详情请见

用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的龟兔赛跑多线程问题

public class Competition {

private volatile static boolean gameOver = false;//用来标记是否有人到达终点,到达终点后游戏结束

//乌龟的实现方式

static class Tortoise implements Runnable{

private volatile int total = 0;//用来记录当前已经前行了多少距离

@Override

public void run() {

while(!gameOver){

int step = (int)(Math.random()*5+1);//产生1-5的随机数

total += step;

try {

Thread.sleep(200);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public int getTotal(){

return total;

}

}

//兔子的实现方式

static class Rabbit implements Runnable{

private volatile int total = 0;

@Override

public void run() {

while(!gameOver){

int step = (int)(Math.random()*5+1);

total += step;

try {

Thread.sleep(200);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public int getTotal(){

return total;

}

}

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

final Tortoise tortoise = new Tortoise();

final Rabbit rabbit = new Rabbit();

new Thread(tortoise).start();

new Thread(rabbit).start();

//下面多起了一个线程,相当于比赛的时候的裁判员,他每隔一段时间就看一下是否有人到达终点,若有人到达则宣判该人获胜,游戏结束

new Thread(new Runnable(){

@Override

public void run() {

// TODO Auto-generated method stub

while(!gameOver){

int torLen = tortoise.getTotal();//获得乌龟前行的距离

int rabLen = rabbit.getTotal();//获得兔子前行的距离

System.out.println("乌龟:"+torLen+",兔子"+rabLen);

if(torLen = 100  rabLen 100){

System.out.println("乌龟获胜!!!");

gameOver = true;

}else if(rabLen = 100  torLen 100){

System.out.println("兔子获胜!!!");

gameOver = true;

}else if(rabLen =100  torLen = 100){//这里有可能两人同时到达终点

System.out.println("同时到达终点!!!");

gameOver = true;

}

try {

Thread.sleep(210);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}).start();

}

java算法题——龟兔赛跑

public class Test1{

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

   int v1=25,v2=10,t=20,s=3,l=100;

          

          

         Scanner scanner=new Scanner(System.in);

           

         v1=scanner.nextInt();    

          v2=scanner.nextInt();

         t=scanner.nextInt();

         s=scanner.nextInt();

         l=scanner.nextInt();

          

         int sum1=0,sum2=0;

         boolean stop=false;

         int stopcount=0;

         int i=0;

         for ( i = 0; i  l/v2; i++) {

             

          

          if(sum1=l||sum2=l)//如果有一个跑到了终点就结束了

          break;

          

             if(stopcount==s)

                 stop=false; //如果休息的时间够了,就开始跑

              

              

             if(sum1-sum2=t)

                 stop=true; //如果超过了t米,就休息

              

              

             if(!stop)

                sum1+=v1; //当兔子不休息的时候跑

             else

                 stopcount++; //休息的时间计数

              

             sum2+=v2;//乌龟每次都会跑

              

              

             System.out.print("兔子跑了:"+sum1+"米");

             System.out.println("乌龟跑了:"+sum2+"米");

        }

         if(sum1==sum2)

             System.out.println("D"+i);

         else if(sum1=l)

             System.out.println("R"+i);

         else if(sum2=l)

             System.out.println("T"+i);

}

}

关于java龟兔赛跑课程设计和龟兔赛跑教学案例的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-11,除非注明,否则均为首码项目网原创文章,转载请注明出处。