Chris7S
Nerd by definition, programmer by trade
I was approached by another student again yesterday that is taking a Java course and he asked how to determine the highest and lowest number in a set array. Since I have already taken a few Java courses and taught myself most of the language I decided to help him out and write him a snippet of code. So whenever a fellow student comes to me for help I will release it here on Se7ensins because I noticed some of the developers here are starting college and they will need to know some of this basic knowledge. I hope this helps someone out in the log run because I know small applications like this really helped me when learning how to code. Enjoy!
And here is the output of the application:
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lowest.and.highest.number;
import java.util.*;
/**
*
* @author Chris
*/
public class LowestAndHighestNumber {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int num1;
int num2;
int num3;
int num4;
int num5;
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter a set of numbers (Max - 5 Numbers):");
num1 = userInput.nextInt();
num2 = userInput.nextInt();
num3 = userInput.nextInt();
num4 = userInput.nextInt();
num5 = userInput.nextInt();
int numbers[] = {num1, num2, num3, num4, num5};
int largestNumber = numbers[0];
int smallestNumber = numbers[0];
for(int i = 0; i < numbers.length; i++)
{
if(numbers[i] > largestNumber)
{
largestNumber = numbers[i];
}
if (numbers[i] < smallestNumber)
{
smallestNumber = numbers[i];
}
}
System.out.println("The largest number in the set {" + num1 + ", " + num2 + ", " + num3 + ", " + num4 + ", " + num5 + "} is " + largestNumber);
System.out.println("The smallest number in the set {" + num1 + ", " + num2 + ", " + num3 + ", " + num4 + ", " + num5 + "} is " + smallestNumber);
}
}
And here is the output of the application:
Last edited: