I was assigned to write a program that read a sequence of integer inputs and print -the smallest and largest of the inputs -and the number of even and odd inputs
I figured out the first part but am stumped on how I can get my program to display the largest and the smallest. This is my code so far. How can I get it to display the smallest input aswell?
public static void main(String args[])
{
Scanner a = new Scanner (System.in);
System.out.println("Enter inputs (This program calculates the largest input):");
double largest = a.nextDouble();
while (a.hasNextDouble())
{
double input = a.nextDouble();
if (input > largest)
{
largest = input;
}
}
System.out.println(largest);
}
The simplest solution would be use something like Math.min
and Math.max
double largest = a.nextDouble();
double smallest = largest;
while (a.hasNextDouble()) {
double input = a.nextDouble();
largest = Math.max(largest, input);
smallest = Math.min(smallest, input);
}