我是Java入门课程的新手,并且正在为程序而苦苦挣扎。该程序应该模拟乌龟和野兔之间的比赛。每个人都有基于随机生成的数字的多种动作(在赛车板上移动的方式)。我对随机数的生成有疑问。以下是我的错误消息。谁能帮我解决错误消息的问题?另外,如果您在我的代码中看到任何其他需要修复的内容或任何其他内容,那么将非常感谢您的帮助。谢谢,这是下面带错误消息的代码。附言我使用blue J编写和编译代码:

import java.util.Random;

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 < 70 && hare < 70)
        {
            MoveHare();
            MoveTortoise();
            DisplayCurrentLocation();
            String request;
        } //end while
        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.next(1, 10);
        //fast plod
        if ( n <= 5)
            tortoise += 3;
        //slip
        else if (n == 9 || n == 10)
            tortoise -= 6;
        //slow plod
        else if (n == 6 || n == 7 || n == 8)
            ++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()
    {
        // randomize move
        int percent = randomGenerator.Next(1, 10);
        // determine moves by graph
        //big hop
        if (percent == 1 || percent == 2)
            hare += 9;
        //big slip
        else if (percent == 6)
            hare -= 12;
        // small hop
        else if (percent == 3 || percent == 5)
            ++hare;
        // )small slip
        else if (percent == 7 || percent == 8)
            hare -= 2;
        else if (percent == 9 || percent == 10)
            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++)
            // same spot
            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 class RaceTest
{
    public static void main ( String[] args)
    {
        Race Application = new Race();
        string request;
        do
     {
        Application.StartRace();
        System.out.println ("");
        System.out.println("Do you want to Play again y/n");
        request = Console.ReadKey();
        if (request == "Y" || request == "y")
        {
            again = true;
        }
        else
        {
            again = false;
        }
     }
    while (again == true);
        System.out.println("Thank you for Playing");
   }
 }
}


错误消息:类java.util.random中的next方法不能应用于给定类型;必填:int,发现:int,int;原因:实际和正式论点清单的长度不同

感谢您的所有帮助。

最佳答案

几个问题。

首先,方法next(int)是受保护的,这意味着它不打算由客户端(Random类的用户)调用。因此,您应该使用方法nextInt()

其次,要生成任意范围内的随机整数,方法nextInt(int bound)与此最接近,它返回0到bound-1(包括1)之间的整数,因此具有相同的作用

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


这将生成一个介于0和9之间的随机数,然后加1以使范围为1..10。



1形式上,它返回范围为[0..bound)的整数

关于java - 随机整数:不能应用于给定类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35387975/

10-12 00:34
查看更多