Hi All,

I am needing some assistance on how I can grab the values for the number of die that come up as 1, 2, 3, 4, 5 or 6. I have the count variables holding the count for each one that fits the "if statement". But I can't use the count variables(onesCnt, twosCnt and etc)within the public static void main part of the code as it is saying "non static variable onesCnt cannot be referenced from a static context". Can someone tell me how I can grab the values so I can print out the output on the screen for the count variables? I tried to create a get method to use the return method to grab the count variables but that didn't work either :(

public class Die 
{

public static final int MAXROLLS = 100;
public int[] dieRollCnt;
private int onesCnt;
public int twosCnt;
public int threesCnt;
public int foursCnt;
public int fivesCnt;
public int sixesCnt;

public static void main(String[] args)
{

   //System.out.println("This concludes the example" + onesCnt);
}



public void die(int[] dot)
{
   System.out.println("We will roll the die 100 times and store the input for the variables");
   int dieRollCount[] = new int[MAXROLLS];
   int oneCount = 0;
   int twoCount = 0;
   int threeCount = 0;
   int fourCount = 0;
   int fiveCount = 0;
   int sixCount = 0;
   for(int i=0; i < dieRollCount.length; i++)
   {
   //System.out.println("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*6));
   dieRollCount[i]  =  (int) (Math.random()*6) + 1;
   //System.out.println("Die Roll [" + (i+1) + "] + dieRollCount[i]");
   System.out.println("Die Roll Number ["+ (i+1) + " + is ] = " + dieRollCount[i]);
   if(dieRollCount[i] == 1 )
   {
       onesCnt += oneCount++;


   }
   else if(dieRollCount[i] == 2)
   {
       twosCnt += twoCount++;
   }
   else if(dieRollCount[i] == 3)
   {
       threesCnt += threeCount++;
   }
   else if(dieRollCount[i] == 4)
   {
       foursCnt += fourCount++;
   }
   else if(dieRollCount[i] == 5)
   {
       fivesCnt += fiveCount++;
   }
   else if(dieRollCount[i] == 6)
   {
       sixesCnt += sixCount++;
   }
   else
   System.out.println("Please contact the developer as the code is not"
   + "functioning correctly");

   } 
}

}

Recommended Answers

All 3 Replies

Since you're not instantiating any objects of class Die, anything you want to use must be declared static. That includes all the variables that are defined directly within the class as well as the die() method.

That said, you don't have code showing how you expect to use the Die class (namely the body of main), so I could be way off base.

Yes that is my question. How can I grab the values that I am incrementing in the if else loops so that I can print out the values? Do I need to restructure everything?

I have fixed the code I just took out the variables I had in the class Die. And just used the variables in the static void main portion to get the value from the counts of the variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.