As you can see here, I return the two largest integers of my array.
int[] a = {a1 = (int) ((Math.random() * 10) + 1), a2 = (int) ((Math.random() * 10) + 1), a3 = (int) ((Math.random() * 10) + 1), a4 = (int) ((Math.random() * 10) + 1), a5 = (int) ((Math.random() * 10) + 1)};
public static int[] showtwolargestints(int a[]) { // returns two largest integers of my array
int largestA = Integer.MIN_VALUE, largestB = Integer.MIN_VALUE;
for(int value : a) {
if(value > largestA) {
largestB = largestA;
largestA = value;
} else if (value > largestB) {
largestB = value;
}
}
return new int[] { largestA, largestB };
}
I managed to use them in an if statement in my GUI class / JButton method.
int[] twoLargest = myobject.showtwolargestints(myobject.a);
public void buttonmethod() {
try {
if (twoLargest[0] == 10 && twoLargest[1] == 10) {
// ...
} else if (twoLargest[0] == 10 && twoLargest[1] == 9) {
// ...
}
However, I now figured out, that for my programm I also have to use every single remaining integer of my array in an if statement after having found these two largest values.
I am a beginner and my attempts did not work out very well :l Could you help me out in a simple way?
PS: I neither want to sum the remaining values (I need every single one) nor want to sort them all by Bubblesort or sth. like that.
Aucun commentaire:
Enregistrer un commentaire