⑴ java 用多线程模拟龟兔赛跑:
public class TortoiseAndHareRace {
public static void main(String[] args) {
Runnable vs=new Race();
Thread hare=new Thread(vs,"Hare");
Thread tortoise =new Thread(vs,"Tortoise");
System.out.println("Ready!GO!");
hare.start();
tortoise.start();
}
}
class Race implements Runnable{
private static final int S=1000;
@Override
public void run() {
if(Thread.currentThread().getName().equals("Hare")){
int sHare=0;
while(sHare<=S){
sHare+=5;
if(sHare%20==0)
try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
}
}else{
int sTortoise=0;
while(sTortoise<=S){
sTortoise++;
if(sTortoise%100==0)
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
⑵ java龟兔赛跑求简单多线程
publicclassCompetition{
=false;//用来标记是否有人到达终点,到达终点后游戏结束
//乌龟的实现方式
{
privatevolatileinttotal=0;//用来记录当前已经前行了多少距离
@Override
publicvoidrun(){
while(!gameOver){
intstep=(int)(Math.random()*5+1);//产生1-5的随机数
total+=step;
try{
Thread.sleep(200);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
publicintgetTotal(){
returntotal;
}
}
//兔子的实现方式
{
privatevolatileinttotal=0;
@Override
publicvoidrun(){
while(!gameOver){
intstep=(int)(Math.random()*5+1);
total+=step;
try{
Thread.sleep(200);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
publicintgetTotal(){
returntotal;
}
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
finalTortoisetortoise=newTortoise();
finalRabbitrabbit=newRabbit();
newThread(tortoise).start();
newThread(rabbit).start();
//下面多起了一个线程,相当于比赛的时候的裁判员,他每隔一段时间就看一下是否有人到达终点,若有人到达则宣判该人获胜,游戏结束
newThread(newRunnable(){
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
while(!gameOver){
inttorLen=tortoise.getTotal();//获得乌龟前行的距离
intrabLen=rabbit.getTotal();//获得兔子前行的距离
System.out.println("乌龟:"+torLen+",兔子"+rabLen);
if(torLen>=100&&rabLen<100){
System.out.println("乌龟获胜!!!");
gameOver=true;
}elseif(rabLen>=100&&torLen<100){
System.out.println("兔子获胜!!!");
gameOver=true;
}elseif(rabLen>=100&&torLen>=100){//这里有可能两人同时到达终点
System.out.println("同时到达终点!!!");
gameOver=true;
}
try{
Thread.sleep(210);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}).start();
}
⑶ JAVA龟兔赛跑问题
先让兔子进入阻塞状态,然后等乌龟跑完终点后唤醒兔子线程就行;下面是各个方法的配套使用(网上的,总结的很不错)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() 被调用。
详情请见http://blog.csdn.net/small____fish/article/details/7726468
⑷ java画乌龟
首先,手动画一个小乌龟,如下:
/**
*功能:画一个乌龟
*/
packagecom.test1;
importjava.awt.*;
importjavax.swing.*;
{
MyPanel2mp=null;
//构造函数
publicMyTortoise(){
mp=newMyPanel2();
this.add(mp);
this.setTitle("小乌龟,丑丑哒");
this.setSize(400,300);
this.setVisible(true);
this.setLocation(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
MyTortoisemtg=newMyTortoise();
}
}
//我的面板。只有JPanel有画图方法,JFrame没有,故必须在JFrame中添加JPanel
classMyPanel2extendsJPanel{
//定义一个乌龟
Tortoiset=null;
//构造函数
publicMyPanel2(){
t=newTortoise(100,100);
}
//画乌龟
publicvoiddrawTortoise(intx,inty,Graphicsg){
//1.画脸
g.setColor(Color.green);
g.fillOval(x+60,y,30,15);
//2.画左眼
g.setColor(Color.black);
g.fillOval(x+65,y+3,5,5);
//3.画右眼
g.fillOval(x+78,y+3,5,5);
//4.画脖子
g.setColor(Color.green);
g.fillOval(x+70,y,10,42);
//5.画乌龟壳
g.setColor(Color.red);
g.fillOval(x+40,y+40,70,100);
//6.画左上脚
g.setColor(Color.green);
g.fillOval(x+15,y+60,30,10);
//7.画右上脚
g.fillOval(x+105,y+60,30,10);
//8.画左下脚
g.fillOval(x+15,y+110,30,10);
//9.画右下脚
g.fillOval(x+105,y+110,30,10);
//10.画尾巴
g.setColor(Color.black);
g.drawLine(x+70,y+140,x+130,y+210);
g.drawOval(x+95,y+150,30,30);
}
//覆盖JPanel的paint方法
//Graphics是绘图的重要类。你可以把他理解成一只画笔
publicvoidpaint(Graphicsg){
//1.调用父类函数完成初始化任务
//这句话不能少
super.paint(g);
//2.画乌龟,调用方法即可
this.drawTortoise(50,50,g);
}
}
//定义一个乌龟类
classTortoise{
//表示乌龟的横坐标
intx=0;
//表示乌龟的纵坐标
inty=0;
publicintgetX(){
returnx;
}
publicvoidsetX(intx){
this.x=x;
}
publicintgetY(){
returny;
}
publicvoidsetY(inty){
this.y=y;
}
publicTortoise(intx,inty){
this.x=x;
this.y=y;
}
}
⑸ 如何用JAVA编一个抽乌龟的小游戏
import java.util.*;
public class A
{
public static void main(String[] args)
{
int count=5;
int[] num={1,2,3,4,5,6,7,8,9,10};
Scanner s=new Scanner(System.in);
int temp=(int)(Math.random()*10)+1;
System.out.println("您的乌龟年龄是:"+temp+"岁的.");
while(count>0)
{
Scanner ss=new Scanner(System.in);
System.out.println("请抽牌,您还有"+count+"次机会.");
System.out.println("开始抽牌--> y/n");
String sss=ss.nextLine();
if(sss.equals("y"))
{
int info=(int)(Math.random()*num.length);
if(num[info]==temp)
{
System.out.println("恭喜,您抽中了");
break;
}
else
{
System.out.println("没抽中");
}
}
else
{
break;
}
--count;
}
}
}
⑹ java多线程龟兔鸡赛跑
你可能是bw 的学生 吧 是不是表
⑺ 用java画一个乌龟,能用键盘移动,并能翻身!
用画布去话,跟画验证码一样。只不过你乌龟有点麻烦。
实现他移动很简单,监听键盘就好了,翻身的话,换张图片,就到达效果了。
⑻ java基础代码,求问那句maria.doSth(jose)的影响,两个乌龟分别怎么动(蓝色是jo
我认为答案应该是 B。
首先需要搞清楚题主提到的
voiddoSth(Turtleturtle);
方法,此方法接受一个 Turtle 类的对象 turtle 作为参数,但请注意这里传递进来的 turtle 参数并不是真正的对象 turtle,而是 turtle 对象所在的内存地址(类似于 C 语言的指针)。
举个例子,此程序的 main 函数中使用
Turtlejose=newTurtle(150,50,world);
建立了一个新的 Turtle 类的对象,假设这个对象被存储在内存地址 74 当中,则变量 jose 的值是这个 74 而不是新建的整个对象,也就是说,这个语句的意思是“在内存地址 74 中新建一个 Turtle 类的对象,并将其所在的内存地址 74 赋予一个叫 jose 的变量”。
Java 会在你调用 jose 的方法时,自动访问 jose 指向的这个内存地址 74 中的实际的对象并调用其方法,而不必像 C 语言一样需要手动进行解除引用的步骤。比如如果运行
jose.turn(90);
的话,实际上 Java 所进行的操作是“访问变量 jose 内存储的内存地址 74,找到地址 74 中存储的 Turtle 类的对象,并调用此对象的 turn 方法。”
了解了这个原理后,就可以看
maria.doSth(jose);
这一句了。同前面所说,虽然 maria 存储的只是真正的对象的内存地址,但在调用方法时 Java 会自动访问这个地址并找到真正的对象,所以这一句会调用真正的 maria 对象的 doSth 方法,并将 jose 传递进去。同样,请注意这里传进来的 jose 是真正的 jose 对象所在的内存地址(延续上面的假设,内存地址 74)。
这一行,Java 首先会将 jose 内的内存地址(74)复制一份并赋予一个新的本地变量 turtle,这样做是为了防止在方法中意外改变 jose 本身的值,从而造成指针指向的错乱。所以这一行之后就多出了一个本地变量 turtle,它的值是 74,指向 jose 对象。
接下来就进了入 doSth 方法的执行。此方法的第一行
turtle=this;
将 this 所指向的对象(this 就是调用此方法的对象本身)所在的内存地址赋予 turtle。具体到这里就是将 maria 对象所在的内存地址的值赋予 turtle。假设 maria 对象所在的内存地址是 68,那么这行之后 turtle 的值就从原来的 74 变成了 68。也就是说 turtle 变量现在不再指向 jose 对象,转而指向了 maria 对象。
接下来的
this.forward(20);
turtle.turn(-90);
this.forward(20);
可以解释为以下三个动作:
将 this 所指向的内存地址中的对象找出并调用其 forward 方法。
将 turtle 所指向的内存地址中的对象找出并调用其 turn 方法。
将 this 所指向的内存地址中的对象找出并调用其 forward 方法。
因为 this 所指向的内存地址中的对象就是 maria 对象本身,而 turtle 由于 doSth 方法第一句的赋值也指向了 maria 对象本身,所以这三条指令实际上全部是作用于 maria 身上的,和 jose 没有任何关系。也就是说,
maria.doSth(jose);
相当于
maria.forward(20);
maria.turn(-90);
maria.forward(20);
以上就是 doSth 方法运行的说明。了解这个之后就可以按照程序给出的顺序走一遍然后确定两只乌龟分别的路径了。步骤为:
Worldworld=newWorld(200,200);
新建一个 200 * 200 的地图:
可见最终的路径是 B。
⑼ 如何用最基础的JAVA语言编写乌龟爬行代码
很简单,看文库代码即可:
http://wenku..com/view/a9213da9dd3383c4bb4cd24e?fr=prin