I need to get the maximum value out of this code, but it’s really screwy. Anyone got any suggestions?
import java.util.Scanner;
public class Test{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
final int sentinel = 0;
int number;
int maxval = 0;
int count = 0;
System.out.println ("Enter a number: ");
number = scan.nextInt();
while (number != sentinel)
{
count++;
System.out.println("Enter another number");
number = scan.nextInt();
maxval = number;
}
if (number > maxval)
{
maxval = number;
System.out.println ("The max value is " + maxval);
}
}
}
you overwrite the
maxvalvariable every time you get a new number. You have to do something like this