0

Hey guys. I seem to be having a problem with my code.

import java.util.Scanner;


public class fishingGames
{

   public static void main(String[] args)
   {
      int totalPoints = 0;
      String items;
      int Sides = 6;
      boolean playAgain = true;

      Scanner keyboard = new Scanner(System.in);

      Dice = new Dice(Sides);


      while (playAgain)
      {
         fishing.roll();


         if (fishing.getvalue()==1) 
         {
            fish = "A piece of plastic";

            totalPoints+= fishing.getvalue();
         }
         else if(fishing.getValue()==2)
         {
            fish= "A tiny fish";

            totalPoints+=fishing.getValue();
         }
         else if(fishing.getValue()==3)
         {
            fish = "A common fish";

            totalPoints+= fishing.getValue();
         }
         else if(fishing.getValue()==4)
         {
            fish = "A catfish";

            totalPoints+= fishing.getValue();
         }
         else if(fishing.getValue()==5)
         {
            fish = "A rainbow fish!";

            totalPoints+= fishing.getValue();
         }
         else if(fishing.getValue()==6)
         {
            fish = "A very rare fish!!!!!";

            totalPoints+= fishing.getValue();
         }

         System.out.println("You caught" + fish);

         System.out.println("Would you like to keep fishing? [Y or N]");

         char pick = scanner.next().charAt(0);
         if(pick =='n')

         playAgain =false;
      }

      System.out.println("Your points total up to: " + totalPoints);

   }

}

import java.util.Random;

public class die
{
    private int sides;
    private int value;

    public die(int numbSides)
    {
       sides = numSides;

       roll();
    }

    public void roll()
    {
       Rand random = new Random();

       value = rand.nextInt(Sides)+1;  
    }

    public int getSides()
    {
       return Sides;
    }

    public int getValue()
    {
       return value;
    }
}

i keep getting this error, class, interface, or enum expected for line 77. I am sorry if this problem is very easy to solve, I am just starting to learn java and I would appreciate any help I can recieve.

Thank you

2
Contributors
1
Reply
31
Views
1 Year
Discussion Span
Last Post by stultuske
0

Do you have these classes in one single .java file?
Then that is your problem. The second class (with it's import statement) has to be inside a second .java file, being: die.java

But this is far from your only problem: you are using variables you never declare. For instance: what is 'fishing' in the fishingGames class?
and what is 'fish'?

It's obvious that fish is supposed to be a String object, but fishing ... Is this supposed to be your Dice variable?
Next to that, you don't show your Dice class, so it's pretty hard to say whether or not that 'll lead you into trouble. The declaration will, however, since either you don't provide a valid declaration, or you forgot to add the name.

Anyway, a few recommendations. Firstly, use decent naming conventions, it 'll make your code easier to read.
Also, try not to have too much code you don't need. An example:

fishing.roll();
            if (fishing.getvalue() == 1)
            {
                fish = "A piece of plastic";
                totalPoints += fishing.getvalue();
            }
            else if (fishing.getValue() == 2)
            {
                fish = "A tiny fish";
                totalPoints += fishing.getValue();
            }
            else if (fishing.getValue() == 3)
            {
                fish = "A common fish";
                totalPoints += fishing.getValue();
            }
            else if (fishing.getValue() == 4)
            {
                fish = "A catfish";
                totalPoints += fishing.getValue();
            }
            else if (fishing.getValue() == 5)
            {
                fish = "A rainbow fish!";
                totalPoints += fishing.getValue();
            }
            else if (fishing.getValue() == 6)
            {
                fish = "A very rare fish!!!!!";
                totalPoints += fishing.getValue();
            }

totalPoints += fishing.getValue(); is repeated in every single option, so there's no need to repeat the statement.

fishing.roll();
                totalPoints += fishing.getvalue();
            if (fishing.getvalue() == 1)
            {
                fish = "A piece of plastic";
            }
            else if (fishing.getValue() == 2)
            {
                fish = "A tiny fish";
            }
            else if (fishing.getValue() == 3)
            {
                fish = "A common fish";
            }
            else if (fishing.getValue() == 4)
            {
                fish = "A catfish";
            }
            else if (fishing.getValue() == 5)
            {
                fish = "A rainbow fish!";
            }
            else if (fishing.getValue() == 6)
            {
                fish = "A very rare fish!!!!!";
            }

At this point, you still have a lot of lines of code for a simple functionality. Since the values are directly (and fixed) linked to the String to set, it's easier to use an array with the Strings, and check the index.
Possible values you can get: 1 -> 6
ArrayIndices: 0 ->5

String[] fished = {"A piece of plastic","A tiny fish", "A common fish","A catfish", "A rainbow fish!","A very rare fish!!!"};
fishing.roll();
totalPoints += fishing.getvalue();
fish = fished[fishing.getValue()-1];

This does everything all your if-else if blocks do, but will be easier to maintain.
Notice also, that for your fishing variable (the one with either missing or faultive declaration), you sometimes call getvalue(), and other times getValue(). Java is case sensitive, meaning that these two are not the same.

This topic has been dead for over six months. 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.