Design and code a Java console application that takes as input five integer valu
ID: 3146916 • Letter: D
Question
Design and code a Java console application that takes as input five integer values and produces as output the lowest and highest values of these five integer values.
The application uses Java looping constructs to implement its functionality.
The application is to read five integer numbers entered by the user from the keyboard using a Java looping construct.
The application then prints out the highest integer and the lowest integer numbers that were entered.
If the user enters 1 987 23 568 7865, The out put should show highest number as 7865 and lowest number as 1
Explanation / Answer
import java.io.*;
import java.util.*;
public class Numbers{
public static void main(String[] args){
int[] numbers = new int[5];
int count = 0;
int greatest = 0;
int least = 0;
try{
System.out.println("Enter the numbers");
while(count < 5){
numbers[count] = new Scanner(System.in).nextInt();
if(count == 0){
greatest = numbers[count];
least = numbers[count];
}
else{
if(numbers[count] > greatest) greatest = numbers[count];
if(numbers[count] < least) least = numbers[count];
}
count++;
}
System.out.println("Highest number: " + greatest);
System.out.println("Highest number: " + least);
}
catch(Exception ex)
{
System.out.println("Error while reading numbers");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.