跑步编程
1. JAVA编程题龟兔赛跑
class Animal {
public double speed;
public void run(int length) {
System.out.println(length/this.speed);
}
}
class Rabbit extends Animal {
Rabbit(int speed) {
super.speed = speed;
}
@Override
public void run(int length) {
System.out.println("Rabbit time = "+length/this.speed +" seconds");
}
}
class Tortoise extends Animal {
Tortoise(int speed) {
super.speed = speed;
}
@Override
public void run(int length) {
System.out.println("Tortoise time = "+length/this.speed +" seconds");
}
}
public class Match {
public static int length = 100;
private static void begin(Rabbit r,Tortoise t) {
r.run(length);
t.run(length);
}
public static void main(String[] args) {
Rabbit r = new Rabbit(20);
Tortoise t = new Tortoise(5);
begin(r,t);
}
}
2. 用C语言编程:绘制一架小车,在屏幕上来回奔跑
用flash可以吗?C语言貌似有点难
不过我想到一种方法
你用字符绘制成小车
然后统一输出
做一个循环
反复输出
每次输出
空格加1
做成++
当输出当一定数值时
再做成--
这样就应该能像你说的那样来回跑了吧!
3. JAVA的程序设计,设计一个龟兔赛跑的线程类模拟参与赛跑。
感觉挺有趣的,试着写了个~
public static void main(String[] arg) {
new wugui().run();
new tuzi().run();
}
static class wugui {
final int su = 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 += su;
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 su = 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 += su;
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();
}
}