「java龟兔赛跑」java龟兔赛跑案例分析
今天给各位分享java龟兔赛跑的知识,其中也会对java龟兔赛跑案例分析进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、JAVA的程序设计,设计一个龟兔赛跑的线程类模拟参与赛跑。
- 2、java中龟兔赛跑问题结果为什么出现小数点后好多位?
- 3、关于Java 龟兔赛跑的一点问题,请大神帮我加满注释。。
- 4、java算法题——龟兔赛跑
- 5、java龟兔赛跑求简单多线程
- 6、JAVA龟兔赛跑问题
JAVA的程序设计,设计一个龟兔赛跑的线程类模拟参与赛跑。
感觉挺有趣的,试着写了个~
public static void main(String[] arg) {
new wugui().run();
new tuzi().run();
}
static class wugui {
final int sudu = 4;// 乌龟的速度是每秒4米
public static boolean hasEnd = false;// 是否已经跑到终点
public void run() {
new Thread() {
public void run() {
int distance = 0;
while (distance 100) {
try {
Thread.sleep(1000);
distance += sudu;
System.out.println("小乌龟跑了" + distance + "米");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hasEnd = true;
if (tuzi.hasEnd) {
System.out.println("呜呜,差一点点就赢了~");
} else {
System.out.println("胜利是属于有准备的人的,你的自大害了你!-------乌龟赢了");
}
}
}.start();
}
}
static class tuzi {
final int sudu = 5;// 兔子的速度是每秒5米
public static boolean hasEnd = false;// 是否已经跑到终点
public void run() {
new Thread() {
@Override
public void run() {
int distance = 0;// 跑了多少米
boolean hasXiuXi = false;// 是否休息过
while (distance 100) {
try {
Thread.sleep(1000);
distance += sudu;
System.out.println("小兔子跑了" + distance + "米");
if (distance 50 !hasXiuXi) {
System.out.println("小兔子累了,决定休息一会儿~");
Thread.sleep((long) (10000 * Math.random()));
System.out.println("小兔子休息够了,又开始跑了,决一胜负吧!");
hasXiuXi = true;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hasEnd = true;
if (wugui.hasEnd) {
System.out.println("呜呜,早知道就不休息了~");
} else {
System.out.println("哇哈哈,你个战5渣也想赢我~~做梦去吧!!-------兔子赢了");
}
}
}.start();
}
}
java中龟兔赛跑问题结果为什么出现小数点后好多位?
这是浮点数运算误差造成的,在所有计算机程序设计语言中都会出现的,不可避免,具体原因这里三言两语说不清,自己百度。解决办法就是尽量改用整数运算,比如0.1米可以改为1分米,然后用整型变量来储存和运算,最后输出的时候如果要用米为单位,再把结果除以10即可。
关于Java 龟兔赛跑的一点问题,请大神帮我加满注释。。
//其中出现的this,都是指调用成员变量或成员方法或返回对象,看后面跟的参数而定
import java.awt.*;//导入awt包
import java.awt.event.ActionEvent;//导入eventActionEvent包
import java.awt.event.ActionListener;//导入eventActionListener包
import javax.swing.*;//导入swing包
public class Guiturun extends JFrame implements ActionListener{
//新建一个Guitrurn类,该类继承了JFrame类,同时实现了继承了ActionListener接口
JLabel jl1,jl2;//定义两个JLabel标签对象
JButton jb1,jb2;//定义两个JButton按钮对象
JPanel jp;//定义一个JPanel面板
MyPanel mp;//定义一个MyPanel对象
public static void main(String []args){//主方法
Guiturun yi = new Guiturun();}//创建一个Guiturun对象
public Guiturun(){//构造函数
jl1 = new JLabel();//创建JLabel对象
mp = new MyPanel();//创建MyPanel对象
Thread t = new Thread(mp);//实在不想写了,给你标重点了
t.start();
jp =new JPanel();
jb1 = new JButton("开 始");
jb1.addActionListener(this);
jb2 = new JButton("结 果");
jb2.addActionListener(this);
jp.add(jb1);//将jb1组件添加到JPanel面板中
jp.add(jb2);//将Jb2组件添加到JPanel面板中
this.add(mp);
this.add(jp,"South");
this.setSize(1200,500);//设置窗体尺寸
this.setLocation(100,100);//设置窗体坐标位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭方式
this.setVisible(true);}//设置窗体可见
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jb1){
Thread t1 = new Thread(this.mp.tuzi);
t1.start();
Thread t2= new Thread(this.mp.wugui);
t2.start();}}}
class MyPanel extends JPanel implements Runnable{
Tuzi tuzi;
Wugui wugui;
public MyPanel(){
tuzi = new Tuzi(20, 10);
wugui = new Wugui(20, 270);}
public void paint(Graphics g) {
super.paint(g);//调用父类成员方法
g.drawImage(tuzi.image, tuzi.x, tuzi.y, this);
g.drawImage(wugui.image, wugui.x, wugui.y, this);}
public void run() {
while (true){
try {
Thread.sleep(500);
this.repaint();
if(this.tuzi.x = 350){
this.tuzi.speed = 0;}
if(this.wugui.x = 750){
this.tuzi.speed = 12;}
if(this.wugui.x=900){
this.wugui.speed =0;
this.tuzi.speed =0;}}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();}}}}
class Tuzi implements Runnable
{int x;
int y;
int speed;
Image image;
public Tuzi(int x,int y){
image = Toolkit.getDefaultToolkit().getImage("res/image/tuzi.png");
this.x = x;
this.y = y;
speed =12;
}
public void run() {
while(true){
try {
Thread.sleep(500);
this.x+=speed;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();}}}}
class Wugui implements Runnable
{int x;
int y;
int speed;
Image image;
public Wugui(int x,int y){
image = Toolkit.getDefaultToolkit().getImage("res/image/wugui.png");
this.x = x;
this.y = y;
speed = 8;}
public void run() {
while(true){
try {
Thread.sleep(500);
this.x+=speed;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();}}}}
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龟兔赛跑求简单多线程
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龟兔赛跑问题
先让兔子进入阻塞状态,然后等乌龟跑完终点后唤醒兔子线程就行;下面是各个方法的配套使用(网上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龟兔赛跑的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java龟兔赛跑案例分析、java龟兔赛跑的信息别忘了在本站进行查找喔。
发布于:2022-11-30,除非注明,否则均为
原创文章,转载请注明出处。