Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Find the Lowest & Highest number in an array!

$
0
0
Hello

I am trying to get my program below to print out the lowest and highest values in an Array of 7 Ints entered by the user.

The program is printing out the highest number fine but it always outputs the lowest number as Zero 0.

 Write a program which uses an array to store the ages of 7 students. The
program should ask the user to enter each age in turn. The program should
end by displaying the age of the youngest student, the oldest student, and
the number of students aged 21.*/

import java.util.Scanner;

public class Q5
{
	public static void main (String [] args)
	{
		Scanner kb = new Scanner(System.in);
				
		int[] arrAge = new int[7];
		
		int youngest = arrAge[0];
		int oldest =  arrAge[0];
		
		System.out.println("Please enter each age of the 7 students");	
		
			for(int i = 0; i <arrAge.length; i++)
			{
				System.out.print( (i+1) + ": ");
				arrAge[i] = kb.nextInt();
		
				if(arrAge[i] < youngest)
				{
					youngest = arrAge[i];
				}
				if(arrAge[i] > oldest)
				{
					oldest = arrAge[i];
				}
			}
			
			System.out.println("The lowest age is: "+ youngest); 
			System.out.println("The highest age is: "+ oldest);	
	}
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>