1

I'm trying to simulate a game of Yahtzee by using various methods to roll, compare, and sum up dice.

This is an assignment and I'm required to use these three static methods to do all of my work in.

When I try to run my code however I get this error

exit status 1
Main.java:6: error: non-static method rollDice(int[]) cannot be referenced from a static context
   rollDice(dice);
   ^
Main.java:7: error: non-static method rollDice(int[]) cannot be referenced from a static context
   dice= rollDice(dice);
         ^
2 errors  

I tried running the methods before assigning the values they return to variables but that didn't seem to work. Here is my complete code:

class Main {
  public static void main(String[] args) {
  // int array to hold 5 dice

   int dice[] = new int[5];
   rollDice(dice);
   dice= rollDice(dice);

   fiveOfaKind(dice);
   int points = fiveOfaKind(dice);

   getChance(dice,points);
   int printpoint = getChance(dice,points);
   // You may roll the dice up to 3 times to try to get 5 of a Kind 
   // If you get 5 of a Kind, stop if not keep trying.
   // If you do not get 5 of a Kind after the 3rd roll, you must take the 
   // CHANCE score. 

   System.out.println("Your score is " + printpoint);



  }// end of main method

  public int[] rollDice(int dice[]){
    // generate 5 random numbers / update dice array
     for (int i = 0; i < dice.length; i++) {
      dice[i] = (int)(Math.random() * 6 + 1);
     }
    return dice;

  }// end of rollDice - rolls 5 dice and puts them in an array
   // All 5 dice must be rolled each time

  public static int fiveOfaKind(int dice[]){
     int pts = 0;
     int rNum=0;

    for(int i=0; rNum<3; rNum++){
    if(dice[0]==dice[1]&&dice[1]==dice[2]&&dice[2]==dice[3]&&dice[3]==dice[4]&&dice[4]==dice[5]&&dice[5]==dice[6]){
      pts=50;
    }
    else if(rNum==3){
      pts=0;
    }
    else{
      int[] rollDice;
    }

    }//end of forloop

    // use Array dice - evaluate if all elements are equal
    // if true = pts = 50
    // if false - pts = 0


    return pts;

  }// end of fiveOfaKind - evaluates the dice roll to see if all 5 are equal
   // Returns 50 points if they do

  public static int getChance(int dice[], int points){

    if(points<50){
       points=0;
      for (int i = 0; i < dice.length; i++) {
         points+=dice[i];
      }
    }
    // use Array dice - sum the elements of the Array
    // pys = calculated value of the sum

    int p = points;

    return p;

  }// end of getChance - adds the total value of the dice
    // Returns the total points calculated


}// end of  class
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
Voxle
  • 29
  • 3

3 Answers3

0

You need to make rollDice() method as static. Non static method cannot be called from static Method. If you want to call non static method , you have to use the instance of the class to call that method.

Nitin Kumavat
  • 28
  • 1
  • 6
0

To put it simply, static methods/fields are more like properties associated with the class and can be shared between objects of the class.

public int[] rollDice method doesn't have an existence unless you create an object of the class enclosing the method because it's non-static. Non-static methods are simply associated with each and every object you create. So, the objects you create will have their own set of properties that are not dependent on other objects and you can use these properties to maintain a state within that object.

Simple fix:

Make the method rollDice static (Recommended based on your program structure)

or

Call the method like this:

Main obj = new Main();
obj.rollDice(dice);

I will recommend you go through this article (Static vs Non-Static) if you don't know the difference between the two. It's really important to understand it in terms of Object-oriented programming.

Afridi Kayal
  • 2,112
  • 1
  • 5
  • 15
0

Static methods are generalised and the same for all of the objects and the class that is they are shared and for them it is not compulsory to make an object of that class just calling the function by using class is sufficient but on the other hand for non-static methods it is important to make the object of that class because they are associated with the objects and can only be called using the object of the class

So you require to make a function of the class Main and then use it to call the rollDice function

Eg:
Main ob1=new Main();

 ob1.rollDice(dice);