Java JOptionPane错误--乌龟和毛发赛跑

Java JOptionPane错误--乌龟和毛发赛跑,java,methods,compilation,compiler-errors,joptionpane,Java,Methods,Compilation,Compiler Errors,Joptionpane,我是Java入门课程的新手,正在努力学习一个程序。我以前也发过关于这个项目的帖子,但这是一个不同的问题。我的程序应该模拟兔子和乌龟之间的比赛。我想我有我所需要的一切,但我的JOptionPane短语有问题。我认为我的短语还可以,但我在陈述的while部分遇到了问题。以下是错误消息:找不到符号-变量OK。 我使用Blue J编写和编译我的程序。有没有什么原因使它不起作用?我认为变量OK是程序用户选择启动程序的东西。我弄错了吗?有人能帮忙解决这个问题吗?您在我的程序中看到了其他需要修复的内容吗?谢谢

我是Java入门课程的新手,正在努力学习一个程序。我以前也发过关于这个项目的帖子,但这是一个不同的问题。我的程序应该模拟兔子和乌龟之间的比赛。我想我有我所需要的一切,但我的JOptionPane短语有问题。我认为我的短语还可以,但我在陈述的while部分遇到了问题。以下是错误消息:找不到符号-变量OK。 我使用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);
    }
   }
import java.util.Random;
导入javax.swing.JOptionPane;
阶级竞赛
{
int[]种族=新int[70];
乌龟;
内部共享;
Random randomGenerator=新的Random();
公共布尔值再次=真;
public void StartRace()
{
乌龟=1;
兔子=1;
System.out.println(“它们关闭了!!!!”);
而(乌龟<种族长度&兔子<种族长度)
{
moveHare();
移动乌龟();
DisplayCurrentLocation();
字符串请求;
} 
如果
(乌龟>兔子)
{
System.out.println(“\n乌龟赢!!”);
}
否则如果
(兔子>乌龟)
{   
System.out.println(“\n兔子赢!!!”);
}
否则如果
(兔子=乌龟)
{
System.out.println(“TIE!!!”);
}
}
公龟()
{
int n=randomGenerator.nextInt(10)+1;
//快走
如果(n>0&&n<6)
乌龟+=3;
//滑倒
否则如果(n>10&&n<11)
乌龟-=6;
//慢吞吞
否则如果(n>6&&n<9)
++乌龟;
//防止从一开始就走过去
如果(乌龟<1)
乌龟=1;
//确保比赛结束
否则,如果(乌龟>70)
乌龟=70;
}//乌龟
公屋
{
int m=randomGenerator.nextInt(10)+1;
//大跃进
如果(m>0&&m 3&&m<5)
++野兔;
//)小失误
否则如果(m<9)
兔子-=2;
否则如果(m<11)
兔子+=0;
//确保兔子没有跑过起点
如果(兔子<1)
兔子=1;
//确保兔子不会跑过终点
否则,如果(兔子>70)
兔子=70;
}//结束移动
public void DisplayCurrentLocation()
{
//这是阵列上每个阵列的位置

对于(int count=1;count,使用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_选项,它表示一个int

还有一些代码方面的帮助:在开始时,您初始化长度为70的int[]race

if(tortoise < 70 && hare < 70){}
可以这样说:

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

    }
将返回一个从0到10的数字。请尝试以下操作:

int m = randomGenerator.nextInt(10) + 1;
wich将返回一个从0到9的数字,加上1将得到一个介于1和10之间的数字


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

Java中没有自动定义的变量(除了
this
super
)。如果您想在代码中引用
OK
,您必须在某处声明它。谢谢您的帮助。我更新了我的代码(编辑我的帖子以显示带有您建议的新代码)以上。我仍然收到错误消息。错误消息是“非法的表达式之星--if(n>0&&<6)”你知道我为什么会出现这个错误吗?这是一个带有>和<符号的每个if的错误。我试图按照你上面的建议简化代码。现在看起来怎么样?还有什么需要更改或修复的吗?非常感谢你的帮助。我真的很感激!我更改了它,所以现在应该是正确的。我想我已经接近完成这个项目了因为我得到的错误消息较少。我现在得到的一条消息是“变量startRaceRequest可能尚未初始化”,而“而(startRaceRequest!=JOptionPane.OK_OPTION){}”有什么想法吗?感谢您的持续帮助。我增加了您的答案,并将检查它,以便显示其答案。我只是希望在解决这些问题结束时这样做。有没有其他方法我可以给您反馈?因为我理解开始编程可能会非常混乱,我不能一直很容易地喂您solu不要期望任何人对谷歌可以轻松回答的问题进行如此详细的讨论。但是,因为你似乎已经接近了:在while循环上方添加这一行:“int startRaceRequest=JOptionPane.showConfirmDialog(null,“Message”);“应该这样做了;”
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

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