-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
14
Replies
63
Views
5 Months
Discussion Span
Last Post by JamesCherrill
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");
        }
}
0

small fix my teacher said I needed to do

 public void DisplayCurrentLocation()
        {
            if (tortoise < hare)    // leading blanks
            {
                for (int i=0;i<tortoise;i++)
                    System.out.print("_");
                System.out.print("T");
                for (int i=tortoise;i<hare;i++)
                    System.out.print("_");
                System.out.print("H");
                for (int i=hare;i<50;i++)
                    System.out.print("_");
            }
            else if (tortoise > hare)
            {
                for (int i=0;i<hare;i++)
                    System.out.print("_");
                System.out.print("H");
                for (int i=hare;i<tortoise;i++)
                    System.out.print("_");
                System.out.print("T");
                for (int i=tortoise;i<50;i++)
                    System.out.print("_");
            }
            else
            {    for (int i=0;i<hare;i++)
                System.out.print("_");
                System.out.print("OUCH");
                for (int i=hare;i<50;i++)
                    System.out.print("_");
            }
            System.out.println();
        }
0

I can't imagine why. Your code is clearer, simpler, shorter. Just one small error in line 129 where you forgot to print "_" Did your teacher give any justification for preferring a longer more repetitive version that also has bugs?

(You both forgot to update i after printing OUCH, so you will get 3 underscore too many, but teacher also has 50 instead of 70)

A few points you teacher would have been better commenting on (not actual errors, but could be improved)
you have a race array that you never use
you shouldn't call a variable percent if its values are 1-10
lines 149-156 shoud be written more simply again = (request == 1);
line 157 shoud be written more simply while (again);
(even better) you don't need again, just while (request == 1);
most (all?) Java standards requre the {} round loop/if/else blocks - eg where does the statement starting on line 144 end?
your positioning of { and } follows the original C standard. Normal Java practice (as used in the API code, the documentation and the Oracle training) puts the { on the same line as if/for etc thus showing more actual code in a fixed-height editor window.

To get your grades you obviously have to do what the teacher says, but privately you should be aware that teachers are not always experts in whatever they happen to be teaching today :)

Edited by JamesCherrill

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.