I'm quite new on Java and I'm facing an issue in this excercise: Write a method, findMax(), that repeatedly reads in integers until a 0 integer is read and keeps track of the largest integer that has been read. findMax() then returns the largest number entered.
My problem is that my code (below) is working when the input contains at least one positive integer but it is not working when the input contains only negative integers:
import java.util.Scanner;
public class FindMaxInSeq {
public static int max() {
Scanner scanner = new Scanner(System.in);
int maxSoFar = 0;
int currValue;
do {
currValue = scanner.nextInt();
if (currValue > maxSoFar) {
maxSoFar = currValue;
}
}
while (currValue != 0);
return maxSoFar;}
public static void main(String[] args) {
System.out.println("Test your code here!\n");
FindMaxInSeq test = new FindMaxInSeq();
System.out.println(test.max());
}
}