if(max.hasNextInt())
{
num = max.nextInt();
}
How do I find the max number when using a scanner and sentinel values? my method only displays the last integer inputted. I've been at this for hours and have put my pride to sleep, I need help....please help..........
You condition that tests if the current number is higher than the current maximum is in the wrong place.
Change
if(max.hasNextInt())
{
num = max.nextInt();
}
else
{
if(num > totmax)
{
totmax = num;
}
...
to
if(max.hasNextInt())
{
num = max.nextInt();
if(num > totmax)
{
totmax = num;
}
}
else
{
...
The problem is that you are checking if the number is bigger then the current maximum in the else
clause. Which won't be entered when the user inputted a number (because than it entered the if
clause).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.