Search code examples
javaarraysdice

How to stock in an Array the points of a Dice? java


My aim is to stock how many times a dice has faced a number between 1 and 6 for 10 throws.

It should be something like:

1 = 3 time(s)

2 = 4 time(s)

3 = 0 time(s)

4 = 1 time(s)

5= 1 time(s)

6 = 1 time(s)

Total = 10 times (or throws)

I've Written so far this code

    int final FACES = 6;
    int final THROWS = 10;
    int nbTimes[] = new int[FACES];
    int Face;


    ...


    for(int thr = 1; thr < THROWS ; thr++){
        Face = throwDice();
        if (Face == Face)
        nbTimes[Face] +=1;
    }

Where throwDice() gives me a random number between 1 and 6. I have this already done.


Solution

  • The following line will always return true and is redundant:

    if (Face == Face)
    

    If you'll remove this line (and its predecessor) it'll work fine (as well):

    ...
    
    ...
    int final FACES = 6;
    int final THROWS = 10;
    int nbTimes[] = new int[FACES];
    int Face;
    
    ...
    
    for(int thr = 0; thr < THROWS ; thr++){ // int thr = 0 !!! (if you want 10 throws)
        nbTimes[throwDice()] += 1;
    }