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
15.1k22 gold badges59 silver badges53 bronze badges
asked May 1, 2015 at 8:00
1

2 Answers 2

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]);
answered May 1, 2015 at 8:12

Comments

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.

answered May 1, 2015 at 8:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.