Final answer:
The provided code snippet is an implementation of a method named findMax()
Step-by-step explanation:
The provided code snippet is an implementation of a method named findMax() which reads in integers until a negative integer is entered and keeps track of the largest integer entered. The method returns the largest number entered.
To solve the issue of the infinite loop, you need to update the value of the num variable inside the while loop by reading the next integer using input.nextInt(). Otherwise, the loop will run infinitely with the same value num. Add the line num = input.nextInt() inside the while loop, below the if statements.
Here's the corrected code:
public int findMax() {
Scanner input = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int num = input.nextInt();
while (true) {
if (num < 0) {
break;
}
if (num > max) {
max = num;
}
num = input.nextInt();
}
return max;
}