我的乌龟&;Hare Java项目赢得';行不通
我必须为我的Java类做这件事,在Java类中我制作了一个小程序来显示龟兔赛跑。以下是作业: 这个项目包括编写一个小程序来模拟龟兔赛跑。这个 参赛者将沿着一条水平的赛道进行比赛,每条赛道都有一条车道 是的。课程应至少有50个正方形或位置…您可以添加 如果你愿意的话,请多加。比赛开始时,每个参赛者都在各自的位置1 各自车道。第一个到达或通过最后一个方格的竞争者 该球场是比赛的胜利者。 下表显示了每个竞争者可以采取的行动类型我的乌龟&;Hare Java项目赢得';行不通,java,applet,Java,Applet,我必须为我的Java类做这件事,在Java类中我制作了一个小程序来显示龟兔赛跑。以下是作业: 这个项目包括编写一个小程序来模拟龟兔赛跑。这个 参赛者将沿着一条水平的赛道进行比赛,每条赛道都有一条车道 是的。课程应至少有50个正方形或位置…您可以添加 如果你愿意的话,请多加。比赛开始时,每个参赛者都在各自的位置1 各自车道。第一个到达或通过最后一个方格的竞争者 该球场是比赛的胜利者。 下表显示了每个竞争者可以采取的行动类型 Contender Type of Move Percentage of
Contender Type of Move Percentage of Time Result of Move
Tortoise Fast plod 50% 3 squares to right
Slow plod 30% 1 square to right
Slip 20% 6 squares to left
Hare Big hop 20% 9 squares to right
Small hop 30% 1 square to right
Big slip 10% 12 squares to left
Small slip 20% 2 squares to left
Fall asleep 20%
每个竞争者都从位置1开始。当竞争者滑倒时,他们不会滑倒
比位置1更左。您将使用随机数生成器进行模拟
表中显示的每种移动的百分比。
生成范围为1的随机整数n≤ N≤ 10对于乌龟,执行以下操作:
如果数字为1-5,则快速嘟嘟,如果数字为6-8,则缓慢嘟嘟,如果数字为1-5,则滑动
号码是9-10。对于兔子,如果数字为1-2,则执行大跳跃,即小跳跃
如果数字是3-5,如果数字是6,则大滑倒,如果数字是7-8,则小滑倒,
如果号码是9-10就睡着了。
您必须跟踪每个竞争者的位置,并显示每个竞争者的位置
使用图形图像定位。龟兔罐头的图形图像
可在课程下载文件夹中找到。你可能需要调整你的头发的长度
课程和小程序窗口的大小,以获得正确的外观。
提示:在处理问题之前,先了解程序的总体逻辑
图样您可能会发现构建结构很有帮助(但并非绝对必要)
基于模拟时钟的总体控制流程。每次时钟“滴答”的时候
竞争者开始行动
我的程序没有给出错误信息,但当我试着运行它时,我得到一个空白图像,上面有两个带一组分割的矩形,上面写着“Race”和“Turn Null”。我已经尽了最大的努力找到了错误,但我想它可以使用另一双眼睛。如果可能的话请帮忙
这是我的密码:
import java.awt.*;
import java.applet.*;
public class TortoiseAndHareProject extends Applet {
Image tortoise, hare; //creates two images, tortoise and hare
int tortX = 250, hareX = 250; //sets them both to start at pixel 250
final int tortY = 100, hareY = 300, WIDTH = 15, HEIGHT = 50; //sets finals tortY, hareY, WIDTH, and HEIGHT = variables to use in the race
//WIDTH will serve as a counter for distance
int turn;
String turnNum;
int move;
String tMove, hMove;
public void init() {
tortoise = getImage(getDocumentBase(), "tortoise.gif");
hare = getImage(getDocumentBase(), "hare.gif");
move = 0;
turn = 0;
}
public void control() throws InterruptedException {
while ((tortX < 985) || (hareX < 985)) { //while neither has won the race yet (985 = finish)
move = (int) (10 * Math.random());
switch (move) {
case 1:
case 2:
tortX += (3 * WIDTH);
hareX += (9 * WIDTH);
tMove = "Fast Plod";
hMove = "Big Hop";
break;
case 3:
case 4:
case 5:
tortX += (3 * WIDTH);
hareX += WIDTH;
tMove = "Fast Plod";
hMove = "Small Hop";
break;
case 6:
tortX += WIDTH;
if (hareX == 250) { //nothing happens, the hare just doesn't move
} else if (hareX <= (250 + (11 * WIDTH))) //if hare is less than 12 squares ahead of 250
hareX = 250; //the hare just goes back to 250
else
hareX -= (12 * WIDTH); //the hare just moves back 12 * WIDTH
tMove = "Slow Plod";
hMove = "Big Slip";
break;
case 7:
case 8:
tortX += (1 * WIDTH);
if (hareX == 250) { //nothing happens, the hare just doesn't move
} else if (hareX <= (250 + (WIDTH))) //if hare is less than 2 squares ahead of 250
hareX = 250; //the hare just goes back to 250
else
hareX -= (2 * WIDTH); //the hare just moves back 2 * WIDTH
tMove = "Slow Plod";
hMove = "Small Slip";
break;
case 9:
case 10:
if (tortX == 250) { //nothing happens
} else if (tortX <= (250 + (5 * WIDTH))) //if the tortoise is less than 6 squares ahead of 250
tortX = 250; //the tortoise just goes back to 250
else
tortX -= (6 * WIDTH);
tMove = "Slip";
hMove = "Fall Asleep.";
break;
}
turn++;
turnNum = (turn + "");
repaint();
Thread.sleep(30);
}
tortX = 985;
hareX = 985;
repaint();
}
public void paint(Graphics screen) {
drawRace(screen);
if (tortX >= 985) {
screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
screen.drawString("Tortoise Wins", 650, 240);
clearCurrent(screen);
fillNext(screen);
} else if (hareX >= 985) { //if hareX has gotten greater than 985
screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
screen.drawString("Hare Wins", 650, 240);
clearCurrent(screen);
fillNext(screen);
} else {
screen.drawString(("Turn " + turnNum), 621, 55);
screen.setFont(new Font("Times New Roman", Font.ITALIC, 12));
if (tMove != null) {
screen.drawString(tMove, 59, 65);
}
if (hMove != null) {
screen.drawString(hMove, 66, 255);
}
clearCurrent(screen);
fillNext(screen);
}
stop();
}
public void clearCurrent(Graphics s) {
s.clearRect(tortX + 1, tortY + 1, WIDTH - 1, HEIGHT - 1);
s.clearRect(hareX + 1, hareY + 1, WIDTH - 1, HEIGHT - 1);
//in order to get a new screen
}
public void fillNext(Graphics s) {
s.fillRect(tortX + 1, tortY + 1, WIDTH - 1, HEIGHT - 1);
s.fillRect(hareX + 1, hareY + 1, WIDTH - 1, HEIGHT - 1);
//in order to fill the screen with the tortoise & hare
}
public void drawRace(Graphics s) {
s.drawRect(250, 100, 750, 50); //draws a rectangle for the race
s.drawRect(250, 300, 750, 50); //draws another rectangle for the race
int lineX = 265, lineYi = 100, lineYf = 150;
for (int i = 1; i <= 98; i++) { //for the duration of the race
if (lineX == 1000) {
lineX = 265;
lineYi = 300;
lineYf = 350;
}
s.drawLine(lineX, lineYi, lineX, lineYf);
lineX += 15;
}
s.fillRect(tortX + 1, tortY + 1, WIDTH - 1, HEIGHT - 1);
s.fillRect(hareX + 1, hareY + 1, WIDTH - 1, HEIGHT - 1);
s.drawImage(tortoise, 59, 80, this);
s.drawImage(hare, 66, 271, this);
s.setFont(new Font("Times New Roman", Font.BOLD, 24));
s.drawString("Race", 250, 55);
}
public void stop() {
}
}
import java.awt.*;
导入java.applet.*;
公共类OrtoiseAndHarProject扩展小程序{
图像乌龟,兔子;//创建两个图像,乌龟和兔子
int tortX=250,hareX=250;//将它们都设置为从像素250开始
final int tortY=100,hareY=300,WIDTH=15,HEIGHT=50;//设置比赛中使用的final tortY,hareY,WIDTH和HEIGHT=变量
//宽度将用作距离的计数器
内翻;
字符串turnNum;
int-move;
字符串tMove,hMove;
公共void init(){
tortoise=getImage(getDocumentBase(),“tortoise.gif”);
hare=getImage(getDocumentBase(),“hare.gif”);
move=0;
匝数=0;
}
public void control()引发InterruptedException{
虽然两人都还没有赢得比赛(985=终点)
move=(int)(10*Math.random());
开关(移动){
案例1:
案例2:
tortX+=(3*宽度);
hareX+=(9*宽度);
tMove=“快速扑通”;
hMove=“大跳跃”;
打破
案例3:
案例4:
案例5:
tortX+=(3*宽度);
hareX+=宽度;
tMove=“快速扑通”;
hMove=“小跳跃”;
打破
案例6:
tortX+=宽度;
如果(hareX==250){//什么也没发生,兔子就是不动
}else if(hareX)请阅读。一个好问题可能是:“这个方法不起作用。我希望它能做Foo,但实际上它能做Bar。”这个问题更像是,“我的程序不起作用。这是整个作业,以及我整个程序的输出。”请把所有的空行都去掉,这样人们就可以看到你的代码了。没人在乎你需要多快的帮助。我给你定好了。请好好看看和学习。我删除了你的99%条评论,因为它们是愚蠢的。只重复代码的评论是毫无意义的,更糟的是:它们在联合中乱七八糟。评论应该说什么。你正在努力完成,而不是你正在做什么来完成它。所有这些空行同样只是浪费空间,从字面上说。不要这样写代码。