One way to find the largest number is simply to go through the array and compare its values:
int maior = Integer.MIN_VALUE;
int indiceMaior = -1;
for (int i = 0; i < grade.length; i++) {
if (grade[i] > maior) {
maior = grade[i];
indiceMaior = i;
}
}
System.out.printf("Maior número é %d, no índice %d", maior, indiceMaior);
The maior
begins with Integer.MIN_VALUE
, which is the smallest value an integer can have in Java (in this case, it is -2147483648
). I start with the lowest possible value, so I guarantee that, no matter what numbers I have in the array, all will be larger or equal to it.
Then I loop, and for each element of the array, I see if it is larger than maior
. That means that during the loop, the variable maior
contains "the largest number I have found so far". When I find a larger number, I update the value of maior
, and also your respective index (since you informed in the comments who wanted the index).
Notice that in the loop I use grade.length
instead of putting the number 3
explicitly. So, if the array size changes, you don’t need to update your for
.
At the end of the loop, just print the values.
Details
If the largest number appears more than once in the array, the index will be that of the first one. For example, if the array is:
int[] grade = { 2, 1, 2 };
In that case, the exit will be:
Maior número é 2, no índice 0
That is, the first 2
will be considered the largest (the one in the third position - in index 2 - will be disregarded).
There’s still one Egde case, which is when the array is like this:
int[] grade = { Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE };
In this case the exit is:
Maior número é -2147483648, no índice -1
As the program never enters if
(since none of the values is greater than Integer.MIN_VALUE
), the index ends up being -1
.
This could be solved in two ways. The first is changing the condition of if
for >=
:
for (int i = 0; i < grade.length; i++) {
if (grade[i] >= maior) {
maior = grade[i];
indiceMaior = i;
}
}
Only now, when there are repeated values, it considers the last occurrence. Therefore, the output is:
Maior número é -2147483648, no índice 2
And in the previous example, that array is { 2, 1, 2 }
, it would also return index 2.
Another way to solve would be to start with the first element (instead of Integer.MIN_VALUE
), and traverse the array from the second position:
int[] grade = { Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE };
int maior = grade[0];
int indiceMaior = 0;
for (int i = 1; i < grade.length; i++) {
if (grade[i] > maior) {
maior = grade[i];
indiceMaior = i;
}
}
Now the way out is:
Maior número é -2147483648, no índice 0
Bonus
If you just want to find the largest number of the array, regardless of the index, there are other alternatives (although I understand that probably is an exercise and the teacher wants it to be done in a loop, but anyway).
Starting with Java 8, it is possible to use the streams:
import java.util.Arrays;
int maior = Arrays.stream(grade).max().getAsInt();
Of course that in the background is a disguised loop (besides having a performance worse than a simple loop), but it’s an alternative.
Another way would be to use one java.util.List
instead of an array, so it is possible to use the method Collections.max
:
List<Integer> notas = new ArrayList<>();
Scanner input = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Insert your grade: ");
notas.add(input.nextInt());
}
int maior = Collections.max(notas);
Finally, for the specific case where the array has only 3 values, nor need loop. Just use Math.max
twice:
int maior = Math.max(Math.max(grade[0], grade[1]), grade[2]);
Look at this example:
[2,5,7]; (5-2=3),(7-2=5)
Conclusion: Every difference that is positive is greater, if negative is less, and if 0 is equal. Formula : Selected value - initial value.– Maury Developer
Thanks for the help!
– user143767