JOptionPane报错--龟速赛跑

JOptionPane error-- Tortoise and Hair race

我是 Java 入门课程的新手,正在努力学习一个程序。我之前发布过有关此程序的信息,但这是一个不同的问题。我的程序应该模拟兔子和乌龟之间的比赛。我想我拥有我需要的一切,但我在使用 JOptionPane 短语时遇到了问题。我认为我的短语没问题,但我在语句的 while 部分遇到了问题。这是错误消息:找不到符号 - 变量正常。 我使用 Blue J 来编写和编译我的程序。有什么原因不工作吗?我认为变量 OK 是程序用户选择启动程序的东西。我错了吗?谁能帮忙解决这个问题?您在我的程序中看到还有其他需要修复的地方吗?谢谢

import java.util.Random;
import javax.swing.JOptionPane;

class Race
{
    int [] race = new int[70];
    int tortoise;
    int hare;
    Random randomGenerator = new Random();
    public boolean again = true;
    public void StartRace()
    {
        tortoise = 1;
        hare = 1;
        System.out.println("AND THEY'RE OFF!!!!");
        while (tortoise < race.length && hare < race.length)
        {
            moveHare();
            moveTortoise();
            DisplayCurrentLocation();
            String request;
        } 
        if 
            (tortoise > hare)
        {
            System.out.println("\n TORTOISE WINS!!");
        }
        else if
            (hare > tortoise)
        {   
            System.out.println("\n HARE WINS!!!");
        }
        else if
            (hare == tortoise)
        {
            System.out.println("TIE!!!");
        }
    }

    public void moveTortoise()
    {
        int n = randomGenerator.nextInt(10) + 1;
        //fast plod
        if ( n > 0 && n< 6)
            tortoise += 3;
        //slip
        else if (n > 10 && n< 11)
            tortoise -= 6;
        //slow plod
        else if (n > 6 && n< 9)
            ++tortoise;
            // protect from going past start
        if (tortoise < 1)
            tortoise = 1;
       // to make sure game ends
        else if (tortoise > 70)
            tortoise = 70;
    }// end tortoise

    public void moveHare()
    {
        int m = randomGenerator.nextInt(10) + 1;
        //big hop
        if (m > 0 && m<3)
            hare += 9;
        //big slip
        else if (m < 6)
            hare -= 12;
        // small hop
        else if (m > 3 && m< 5)
            ++hare;
        // )small slip
        else if (m < 9)
            hare -= 2;
        else if (m < 11)
            hare += 0;
        //ensure hare doesn't go past start
        if (hare < 1)
            hare = 1;
        // ensure hare doesnt go past end
        else if (hare > 70)
            hare = 70;
    } // end movehare

    public void DisplayCurrentLocation()
    {
        //this is the location of each on the array
        for (int count = 1; count <= 70; count++)
            // when at same location
            if (count ==tortoise && count ==hare)
            { 
                System.out.println("OUCH");
            }
            else if (count == hare)
            { 
                System.out.println("H");
            }
            else if (count == tortoise)
            {
                System.out.println("T");
            }
           else
               System.out.println();

    }

    public static void main ( String[] args)
    {
        Race Application = new Race();
        int startRaceRequest;
        while(startRaceRequest != JOptionPane.OK_OPTION)
     {
            JOptionPane.showConfirmDialog(null, "Select OK To Begin the Race!:");
     }
        do
     {
        Application.StartRace();

     } while(startRaceRequest != JOptionPane.OK_OPTION);
    }
   }

关于 JOptionPane 的问题:我建议你使用

JOptionPane.showConfirmDialog(null, "Message");

而不是

JOptionPane.showMessageDialog(null, "Message");

因为它允许您这样做:

    int startRaceRequest;
    while(startRaceRequest != JOptionPane.OK_OPTION)
        JOptionPane.showConfirmDialog(null, "Hit OK to start the race!");

我不太明白你稍后在代码中所说的 "Ok" 是什么意思,但是如果你参考确认的对话框,请尝试使用 JOptionPane.OK_OPTION 代表一个整数。

对您的代码也有一些帮助:在开始时,您将长度初始化为 70 的 int[] 比赛。每当您检查其中一名赛车手是否达到或超过 70 时,您都可以这样做

if(tortoise < 70 && hare < 70){}

这被称为硬编码,您应该尽量避免这种情况,以使您的代码尽可能保持动态。 如果你这样做

if(tortoise < race.length && hare < race.length)

你不必因为改变了比赛的长度而重写一半的代码。

MoveTortoise 和 MoveHare 方法的另一件事。按照惯例,它们应该命名为 moveTortoise 和 moveHare,因为方法 ins Java 以小写字母开头(这适用于所有方法!)。在 if 条件内的这两个方法中,您编写了太多代码。例如:

if (m == 1 || m == 2)
        hare += 9;
    else if (m == 6)
        hare -= 12;
    else if (m == 3 && m == 5) //there is something wrong here, m cant be 5 and 3 ;)
        ++hare;
    else if (m == 7 || m == 8)
        hare -= 2;
    else if (m == 9 || m == 10)
        hare += 0;

可以剪成这样:

if(m > 0 && m < 3){ // if a number is > 0 and < 3 -> number is 1 or 2

    } else if(m < 6){ // if a number is > 0 and > 3 -> if number is < 6 -> number could be 3, 4 or 5

    } else if(m == 6){ 

    } else if(m < 9){ // -> 7 or 8

    } else if(m < 11){ // 9 or 10

    }

此外,您使用了随机数生成器,我认为您的目标是 1 到 10 之间的数字,但是这

int m = randomGenerator.nextInt(11);

将 return 一个从 0 到 10 的数字。试试这个:

int m = randomGenerator.nextInt(10) + 1;

wich 将 return 一个从 0 到 9 的数字,加一将得到 1 到 10 之间的一个数字。

希望这对您有所帮助。如果您的问题得到解答,请记得提供反馈并标记解决方案。