Hi so I'm new at java programming and i'm currently at learning array. So what i'm trying to do is find the maximum and minimum value of an array but for some reason i cant find the minimum value but i can find the maximum value.
Here is my output:
Enter the number of elements: 5
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
The maximum number is:5
The minimum number is: 0
I've used the same statement for getting the max value by only changing the operator. But somehow the output is always zero.
Here is my code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int elements = input.nextInt();
int [] array = new int[elements];
int max = array[0];
int min = array[0];
for(int i = 0; i<elements; i++){
System.out.print("Enter a number: ");
array[i] = input.nextInt();
if(array[i]>max){
max = array[i];
}
if(array[i]<min){
min = array[i];
}
}
System.out.print("The maximum number is:" + max);
System.out.println();
System.out.print("The minimum number is: " + min);
}
}
Any help would be appreciated thanks!