
正文
java乌龟代码 java小乌龟怎么用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java基础代码,求问那句maria.doSth(jose)的影响,两个乌龟分别怎么动(蓝色是jo
jose 不动 ,maria forward(40) turn(-90)
这是java 中的方法传参问题 ,在java中参数类型是引用类型,传的是这个引用参数的引用的副本,在dosth()中,这个引用turtle指向了maria的地址,改变的都是maria值
相关问答
Q1: 求java龟兔赛跑程序代码!
import java.util.*;
public class Test{
public static void main(String[] args)throws Exception {
float t,g,m=0,num=0;
float tt,gg;
int q=0;
Scanner s;
System.out.println("输入兔子跑一圈时间/秒:");
s=new Scanner(System.in);
t=s.nextFloat();
System.out.println("输入乌龟跑一圈时间/秒:");
s=new Scanner(System.in);
g=s.nextFloat();
tt=(float)1/(t*1000);
gg=(float)1/(g*1000);
System.out.println("赛跑开始……");
while(true){
try{
Thread.sleep(10);
}catch(Exception e){}
m+=10;
if((int)((tt-gg)*m)q){
q=(int)((tt-gg)*m);
System.out.println("在第"+m/1000+"秒");
System.out.println("兔子超过乌龟"+q+"圈");
}
}
}}
Q2: 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();
}
Q3: 已知龟兔起点相差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小乌龟怎么用的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








