gpt4 book ai didi

Java 竞赛小程序不工作

转载 作者:行者123 更新时间:2023-12-02 00:03:56 25 4
gpt4 key购买 nike

我在模拟两个竞争对手之间的比赛时遇到问题。这是典型的竞赛程序,您使用随机数生成器来确定竞争对手使用的“ Action ”。正如我下面的代码所示,赛道由 50 个矩形组成,填充的矩形显示了赛道上每个参赛者的位置。例如,有些“ Action ”会让参赛者向右跳 9 格,或向后跳 2 格。当我运行小程序时,只显示初始起始位置;小程序不起作用。我意识到代码很多,但是我需要做什么来解决我的问题?我现在真的很困惑。任何帮助表示赞赏。我只能用AWT,不能用swing。这是类(class)作业:/这是代码:

import java.awt.*;
import java.applet.*;


public class Example extends Applet
{
Image tortoise, hare;
int tortX = 250, hareX = 250;
final int tortY = 100, hareY = 300, WIDTH = 15, HEIGHT = 50;
int turn; String turnNum;
int move; String tMove, hMove;

public void init()
{
tortoise = getImage( getDocumentBase(), "images/tortoise.gif" );
hare = getImage( getDocumentBase(), "images/hare.gif" );
move = 0; turn = 0;
}


public void control()
{
while (( tortX < 985 ) || ( hareX < 985 ))
{
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) {} // DO NOTHING
else if (hareX <= (250 + (11 * WIDTH)))
hareX = 250;
else
hareX -= (12 * WIDTH);
tMove = "Slow Plod"; hMove = "Big Slip";
break;
case 7:
case 8:
tortX += (1 * WIDTH);
if (hareX == 250) {} // DO NOTHING
else if (hareX <= (250 + (WIDTH)))
hareX = 250;
else
hareX -= (2 * WIDTH);
tMove = "Slow Plod"; hMove = "Small Slip";
break;
case 9:
case 10:
if (tortX == 250) {} // DO NOTHING
else if (tortX <= (250 + (5 * WIDTH)))
tortX = 250;
else
tortX -= (6 * WIDTH);
tMove = "Slip"; hMove = "Fall Asleep. Zzz...";
break;
// Hare falls asleep. No action.
}

turn++; turnNum = (turn + "");
repaint();
for (int i = 1; i <= 10; i++)
{
delay();
}
}

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)
{
screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
screen.drawString("Tortoise Wins", 650, 240);
clearCurrent(screen);
fillNext(screen);
}
else
{
screen.drawString(("Turn " + turnNum), 621, 55);
screen.setFont(new Font("Times New Roman", Font.ITALIC, 12));
screen.drawString(tMove, 59, 65); 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);

}

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);

}

public void drawRace( Graphics s )
{
// GENERATES INITIAL GRAPHICS FOR RACE
s.drawRect(250, 100, 750, 50);
s.drawRect(250, 300, 750, 50);
int lineX = 265, lineYi = 100, lineYf = 150;
for (int i = 1; i <= 98; i++)
{
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 delay()
{
for (int i = 0; i < 90000000; i++)
{
}
}

public void stop()
{
}
}

最佳答案

你的第一个问题是你实际上从未“开始”比赛......当然你的init小程序,但是然后,什么都没有......

你的第二个问题是control方法阻塞了事件调度线程,这意味着,当你使用这个方法时,什么都不会被绘制到屏幕上。这是因为事件调度线程还负责调度重绘请求。

你的第三个问题是你违反了paint联系。您有责任调用 super.paint(screen) - Paint 是一个复杂的方法,永远不应该忽略它,除非您有充分的理由这样做。

您的第四个问题是,您使用 Applet 而不是 JApplet。最好忽略 AWT 控件,而选择 Swing 控件。 Swing 更灵活,更容易扩展。

你的第五个问题是你在顶层容器上绘画,从不推荐这样做。您最好使用诸如 JPanel 之类的东西并覆盖它的 paintComponent 方法(不要忘记调用 super.paintComponent)。除此之外,它是双缓冲的,并且会在屏幕更新时减少闪烁。

看看...

关于Java 竞赛小程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14268266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com