My array takes in the sentinel value as an array element and then terminates
I am writing a program that takes in user input and churns out the minimum
value. After 10 inputs the user can type in a negative number as the
sentinel to exit the loop. The problem is this works but for some reason
the loop takes the sentinel value as an array element aswell. Can anyone
tell me whats wrong?
public class java {
public static void main(String[] args) {
{
double[] a=new double[10];
Scanner arr=new Scanner(System.in);
System.out.println("Please enter values");
int size = 0;
int x = 0;
while ( x >= 0 )
{
System.out.println("Enter next value");
x = arr.nextInt();
a[size]=x;
size++;
}
System.out.println( "Minimum Value" );
System.out.println((min(a)));
}
}
public static double min( double[] arr )
{
double minimum= arr[0];
for (int i=1; i<arr.length; i++)
{
if (arr[i] < minimum)
{
minimum= arr[i];
}
}
return minimum;
}
No comments:
Post a Comment