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
-
Please see: stackoverflow.com/questions/1806816/… Or: stackoverflow.com/questions/16325168/…pev.hall– pev.hall2015年05月01日 08:05:46 +00:00Commented May 1, 2015 at 8:05
2 Answers 2
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
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
.
Comments
lang-java