Hi! I'm trying to create a program that will allow the user to input 5 numbers
and will print the largest and smallest number.
I tried doing it and this is what I came up.. The thing is, after the program executes its purpose i want to add a statement that will allow you to loop the program or ends it.
I'm having a hard time trying to figure out where to put the looping statement and what kind. Can anyone post your suggestion..
and also if you have any suggestion in what to do to simplify this program your welcome to do so :)
import java.util.Scanner;
public class LargestAndSmallestNumber {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
// Variable Declaration
float num; //variable to hold the current number
float max; //variable to hold the largest number
float min; //variable to hold the smallest number
int count; //loop control variable
System.out.println("This program accepts 5 input numbers and will" +
" print the highest and lowest number.");
System.out.print("Enter 5 integers: ");
num = console.nextFloat();
max = num;
min = num;
for (count = 1; count < 5; count++)
{
num = console.nextFloat();
max = larger(max,num);
min = smaller(min,num);
}//end of for statement
System.out.println("******************************");
System.out.println("The largest number is " + max);
System.out.println("The smallest number is " + min);
System.out.println("******************************");
}
public static float larger(float x, float y)
{
float max;
if(x >= y)
max = x;
else
max = y;
return max;
}//end of larger method
public static float smaller(float a, float b)
{
float min;
if(a <= b)
min = a;
else
min = b;
return min;
}//end of smaller method
}