In: Computer Science
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1 is entered, stop prompting for numbers and display the maximum number entered by the user.
I am struggling to have my program print the math function. Here is what I have so far:
import java.util.*;
public class MaxSentinel
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a value. Press -1 to stop
prompt.");
int number = 0;
do
{
System.out.println("Enter a value.");
number = input.nextInt();
}
while(number != -1);
}
}
Dear Student,
below i have written the complete java program as per the requirement.
=============================================================
Program:
=============================================================
import java.util.*;
public class MaxSentinel
{
public static void main(String[] args)
{
int max=0;
Scanner input = new
Scanner(System.in);
System.out.println("Please enter a
value. Press -1 to stop prompt.");
int number = 0;
do
{
System.out.println("Enter a value.");
number =
input.nextInt();
if(number ==
-1)
break;
if(max <
number)
max = number;
}while(number != -1);
System.out.println("Max value is
"+max);
}
}
=============================================================
Sample Output:
=============================================================
Kindly Check and Verify Thanks..!!!