-1

I'm having problems compiling my program, I was wondering if someone could tell me where I went wrong and how to fix it. I would be deeply apreaciative of your help!!!! :)

import java.util.Random;

import java.io.Console;

class project2_0
{
    int[] race = new int[70];
    int tortoise;
    int hare;
    Random randomnumbers = new Random();
    boolean again = true;
    public void StartRace()
    {
        tortoise = 1;
        hare = 1;
        Console.WriteLine("ON YOUR MARK, GET SET.... BANG!!!");
        Console.WriteLine("AND THEY'RE OFF!!!!");
        while (tortoise < 70 && hare < 70)
        {
            MoveHare();
            MoveTortoise();
            DisplayCurrentLocation();
            string request;
        } //end while
        if
            (tortoise > hare)
        {
            Console.WriteLine("\n TORTOISE WINS!! YAY!!!!!");
        }
        else if
            (hare > tortoise)
        {
            Console.WriteLine("\n HARE WINS!!!");
        }
        else if
            (hare == tortoise)
        {
            Console.WriteLine ("TIE!!!");
        }
    }
        public void MoveTortoise()
        {
            //to randomize move
            int percent = randomnumbers.Next(1, 11);
            //now determine moves based on graph
            //fast plod
            if (percent >= 1 && percent <= 5)
                tortoise += 3;
            //slip
            else if (percent == 6 || percent == 7)
                tortoise -= 6;
            //slow plod
            else
                ++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 = randomnumbers.Next(1, 11);
            // determine moves by graph
            //big hop
            if (percent == 3 || percent == 4)
                hare += 9;
            //big slip
            else if (percent == 5)
                hare -= 12;
            // small hop
            else if (percent >= 6 && percent <= 8)
                ++hare;
            // )small slip
            else if (percent > 8)
                hare -= 2;
            //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)
                {
                    Console.WriteLine ("OUCH");
                }
                else if (count == hare)
                {
                    Console.WriteLine ("H");
                }
                else if (count == tortoise)
                {
                    Console.WriteLine("T");
                }
                else
                    Console.WriteLine();
        }
            public class RaceTest
            {
                static void main( String[] args )
                {
                    boolean again = true;
                    Race Application = new Race();
                    string request;
                    do
                    {
                        Application.StartRace();
                        Console.WriteLine("");
                        Console.WriteLine("Do you want to Play again y/n");
                        request = Console.ReadLine();
                        if (request == "Y" || request == "y")
                        {
                            again = true;
                        }
                        else
                        {
                            again = false;
                        }
                    }
                    while (again == true);
                    Console.Writeline ("Thank you for Playing");
                }
            }
}

Edited by Daniel_84

4
Contributors
12
Replies
50
Views
6 Days
Discussion Span
Last Post by Daniel_84
Featured Replies
  • Ok, just think of it like this. You have a method that gives you a random int in a range starting at 0 You want to start at 1 That’s just 1+random int 0-9 You know how to get a random int in the range 0-9, and I’m quite certain … Read More

0

sorry my b
i changed it:

import java.util.Random;

import java.util.Scanner;

class project2_1
{
    int[] race = new int[70];
    int tortoise;
    int hare;
    Random randomnumbers = new Random();
    boolean again = true;

    public void StartRace()
    {
        tortoise = 1;
        hare = 1;
        System.out.println("ON YOUR MARK, GET SET.... BANG!!!");
        System.out.println("AND THEY'RE OFF!!!!");
        while (tortoise < 70 && hare < 70)
        {
            MoveHare();
            MoveTortoise();
            DisplayCurrentLocation();
        } //end while
        if
            (tortoise > hare)
        {
            System.out.println("\n TORTOISE WINS!! YAY!!!!!");
        }
        else if
            (hare > tortoise)
        {
            System.out.println("\n HARE WINS!!!");
        }
        else if
            (hare == tortoise)
        {
            System.out.println("TIE!!!");
        }
    }

        public void MoveTortoise()
        {
            //to randomize move
            int percent = randomnumbers.next(1, 11);
            //now determine moves based on graph
            //fast plod
            if (percent >= 1 && percent <= 5)
                tortoise += 3;
            //slip
            else if (percent == 6 || percent == 7)
                tortoise -= 6;
            //slow plod
            else
                ++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 = randomnumbers.next(1, 11);
            // determine moves by graph
            //big hop
            if (percent == 3 || percent == 4)
                hare += 9;
            //big slip
            else if (percent == 5)
                hare -= 12;
            // small hop
            else if (percent >= 6 && percent <= 8)
                ++hare;
            // )small slip
            else if (percent > 8)
                hare -= 2;
            //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 void main( String [] args )
                {
                    boolean again = true;
                    int request;

                    Scanner input = new Scanner(System.in);

                    do
                    {
                        StartRace();

                        System.out.println("");
                        System.out.println( "Do you want to Play again: 1.Yes   2.No" );
                        request = input.nextInt();

                        if (request == 1)
                        {
                            again = true;
                        }
                        else
                        {
                            again = false;
                        }
                    } while (again == true);
                    System.out.println("Thank you for Playing");
                }
            }
}

I keep getting this when I compile:
Trying_to_compile_project2_1.png

0

If I'm the compiler, I look at that line and I look here...

https://docs.oracle.com/javase/8/docs/api/java/util/Random.html

and I look for a function called Next and I don't find anything by that name so I don't even bother looking at the (1,11) parameters.

If I'm a HUMAN trying to compile it, I throw that same error, then say something like...

  1. Remember, Java is case-sensitive. Did you mean next?
  2. If you mean next, there is a next function that takes ONE integer, not two. Do you want that?
  3. Perhaps you wanted nextInt? That too takes ONE integer, not two.
  4. Are you trying to pick a random integer in the range of 1 through 11? If so, use nextInt(int) with a very small code change.
0

Note: The case-sensitive comment was based on the screenshot and the earlier code. Looks like you've already solved that one. Points 2 to 4 still stand.

0

Yeah i'm trying to make the program pick a random integer, but I don't know quite well how to do it...

int percent = randomnumbers.nextInt(11);

would it be like this?

i did it from 1 - 11 on the previous post because I want it to choose from 10 possible values. (at least thats what I wanted to do)

Edited by Daniel_84

0

no, i want a range from 1 ≤ n ≤ 10
Can someone show me what I need to change exactly, Im quite lost at the moment.

Edited by Daniel_84

1

Ok, just think of it like this.
You have a method that gives you a random int in a range starting at 0
You want to start at 1
That’s just 1+random int 0-9
You know how to get a random int in the range 0-9, and I’m quite certain you know how to add 1
:)

0

ok I fixed it and I was able to compile my program

import java.util.Random;

import java.util.Scanner;

class project2_1
{
    int[] race = new int[70];
    int tortoise;
    int hare;
    Random randomnumbers = new Random();
    boolean again = true;

    public void StartRace()
    {
        tortoise = 1;
        hare = 1;
        System.out.println("ON YOUR MARK, GET SET.... BANG!!!");
        System.out.println("AND THEY'RE OFF!!!!");
        while (tortoise < 70 && hare < 70)
        {
            MoveHare();
            MoveTortoise();
            DisplayCurrentLocation();
        } //end while
        if
            (tortoise > hare)
        {
            System.out.println("\n TORTOISE WINS!! YAY!!!!!");
        }
        else if
            (hare > tortoise)
        {
            System.out.println("\n HARE WINS!!!");
        }
        else if
            (hare == tortoise)
        {
            System.out.println("TIE!!!");
        }
    }

        public void MoveTortoise()
        {
            //to randomize move
            int percent = randomnumbers.nextInt(10) + 1;
            //now determine moves based on graph
            //fast plod
            if (percent >= 1 && percent <= 5)
            {
                tortoise += 3;
            }
            //slip
            else if (percent == 6 || percent == 7)
            {
                tortoise -= 6;
            }
            //slow plod
            else
            {
                ++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 = randomnumbers.nextInt(10) + 1;
            // determine moves by graph
            //big hop
            if (percent == 3 || percent == 4)
            {
                hare += 9;
            }
            //big slip
            else if (percent == 5)
            {
                hare -= 12;
            }
            // small hop
            else if (percent >= 6 && percent <= 8)
            {
                ++hare;
            }
            // )small slip
            else if (percent > 8)
            {
                hare -= 2;
            }
            //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 void main( String [] args )
                {
                    boolean again = true;
                    int request;

                    Scanner input = new Scanner(System.in);

                    do
                    {
                        StartRace();

                        System.out.println("");
                        System.out.println( "Do you want to Play again: 1.Yes   2.No" );
                        request = input.nextInt();

                        if (request == 1)
                        {
                            again = true;
                        }
                        else
                        {
                            again = false;
                        }
                    } while (again == true);
                    System.out.println("Thank you for Playing");
                }
            }
}

but i get this when I try to run it Screen_Shot_2018-02-15_at_2_07_59_PM.png
When I added static to the last method the program won't compile

Edited by Daniel_84

0

Your mistake is that class RaceTest is inside another class. Move it out so it’s a top-level class, then you can make your main method static. You will then need to run with RaceTest as the class that you execute and put each public top-level class in its own .java file

If that seems complicated then forget the RaceTest class and put the main method in class Project2_1

Edited by JamesCherrill

0

I did what you said and took out the RaceTest call, but i'm getting this error now

Screen_Shot_2018-02-15_at_5_45_48_PM.png

0

Finish it :)
here is the final program:

import java.util.Random;

import java.util.Scanner;

class project2_1
{
    int[] race = new int[70];
    int tortoise;
    int hare;
    Random randomnumbers = new Random();
    boolean again = true;

    public void StartRace()
    {
        tortoise = 1;
        hare = 1;
        System.out.println("ON YOUR MARK, GET SET.... BANG!!!");
        System.out.println("AND THEY'RE OFF!!!!");
        while (tortoise < 70 && hare < 70)
        {
            MoveHare();
            MoveTortoise();
            DisplayCurrentLocation();
        } //end while
        if
            (tortoise > hare)
        {
            System.out.println("\n TORTOISE WINS!! YAY!!!!!");
        }
        else if
            (hare > tortoise)
        {
            System.out.println("\n HARE WINS!!!");
        }
        else if
            (hare == tortoise)
        {
            System.out.println("TIE!!!");
        }
    }

        public void MoveTortoise()
        {
            //to randomize move
            int percent = randomnumbers.nextInt(10) + 1;
            //now determine moves based on graph
            //fast plod
            if (percent >= 1 && percent <= 5)
            {
                tortoise += 3;
            }
            //slip
            else if (percent == 6 || percent == 7)
            {
                tortoise -= 6;
            }
            //slow plod
            else
            {
                ++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 = randomnumbers.nextInt(10) + 1;
            // determine moves by graph
            //big hop
            if (percent == 3 || percent == 4)
            {
                hare += 9;
            }
            //big slip
            else if (percent == 5)
            {
                hare -= 12;
            }
            // small hop
            else if (percent >= 6 && percent <= 8)
            {
                ++hare;
            }
            // )small slip
            else if (percent > 8)
            {
                hare -= 2;
            }
            //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 static void main( String [] args )
        {
            boolean again = true;
            int request;

            Scanner input = new Scanner(System.in);

            project2_1 d = new project2_1();

            do
            {
                d.StartRace();

                System.out.println("");
                System.out.println( "Do you want to Play again: 1.Yes   2.No" );
                request = input.nextInt();

                if (request == 1)
                {
                    again = true;
                }
                else
                {
                    again = false;
                }
            } while (again == true);
            System.out.println("Thank you for Playing");
        }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.