I'm setting up my array from a user input and then trying to find the min and max.But it only seems to work perfect for finding the max and not the min.
import java.util.Scanner;
public class GradeBookKeeper
{
public static void main(String arg[])
{
int numItems;
int[] items;
double sum=0;
Scanner scan= new Scanner(System.in);
System.out.println("Enter the number of students: ");
numItems=scan.nextInt();
items=new int [numItems];
int largest=items[0],smallest=items[0];
for (int i = 0 ; i < items.length; i++ )
{
System.out.println("Enter the grade for student "+(i+1)+":");
items[i] = scan.nextInt();
sum = sum + items[i];
if(largest < items[i])
{
largest = items[i];
}
else
{
smallest = items[i];
}
}
System.out.printf("The average is "+"%.2f\n",sum/items.length);
System.out.println("The minimum is: "+smallest);
System.out.println("The maximum is: "+largest);
scan.close();
}
}
//output
Enter the number of students: 4
Enter the grade for student 1:
2
Enter the grade for student 2:
1
Enter the grade for student 3:
2
Enter the grade for student 4:
3
The average is 2.00
The minimum is: 2
The maximum is: 3