0

I am writing a program which can tracking three dices' total appears.Here are my codes:

import java.util.*;

class dice{
    public static void main (String[] args){
        Random rnd = new Random();
        int[] track = new int[19];

        for (int roll=0; roll<10000; roll++){
            int sum=0;
            for(int i = 0; i < 3; i++) {
                //roll the 6-face dice
                int n = rnd.nextInt(6) + 1;
                sum+=n;
            }
            ++track[sum];
        }

        System.out.println("Sum\tFrequency");

        for(int finalSum=3; finalSum<track.length;finalSum++){
            System.out.println(finalSum+"\t"+track[finalSum]);
        }
        System.out.println("the largest frequency is %d", //how to find it?)
    }
}

Now I am almost done. But how can I find the largest appearance and print it separately? Thank you.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
turf
  • 183
  • 2
  • 2
  • 16
  • Please see: http://stackoverflow.com/questions/1806816/java-finding-the-highest-value-in-an-array Or: http://stackoverflow.com/questions/16325168/how-would-i-find-the-maximum-value-in-an-array – pev.hall May 01 '15 at 08:05

2 Answers2

1

You can try below code:

Arrays.sort(arr);
System.out.println("Min value "+arr[0]);
System.out.println("Max value "+arr[arr.length-1]);
1
public int largest(int[] myArr) {
    int largest = myArr[0];
    for(int num : myArr) {
        if(largest < num) {
            largest = num;
        }
    }
    return largest;
}

Set the variable largest to the first item in the array. Loop over the array, whenever the current num you're in is bigger than the current largest value, set largest to num.

Brunaldo
  • 66
  • 6