I am pretty new to java and am just learning 2D arrays. I am trying to get the top 5 numbers to display from a random list. I think this could work but am not sure why I am getting an error. One other thing is that I cannot use the sort function.
Code here:
public static void main(String[] args) {
//Random Number stuff
Random rand = new Random();
int[] large = new int [5];
int max = 0, index;
int[][] arrSize = new int [4][5];
for (int i = 0; i < arrSize.length; i++) {
for (int j=0; j< arrSize[i].length; j++) {
arrSize[i][j] = rand.nextInt(89) + 10;
System.out.print(arrSize[i][j] + " ");
}
System.out.println();
}
// Top 5
for (int p = 0; p < 5; p++) {
max = arrSize [0][0];
index = 0;
for (int i = 0; i < arrSize.length; i++) {
for (int j = 0; j < arrSize[i].length; j++) {
if (max < arrSize[i][j]) {
max = arrSize[i][j];
index = i;
}
}
}
large[p] = max;
arrSize[index] = Integer.MIN_VALUE; //Error here
System.out.println("Highest Number: " + large[p]);
}
}
}
Error text:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to int[]
at secondAssignment.BiggestNumbersRectangular.main(BiggestNumbersRectangular.java:47)
I am not sure why I am getting an error, any help would appreciated. If anyone else has any answers for how I could get the top 5 in a different way that would also be appreciated.
Aucun commentaire:
Enregistrer un commentaire