1

when I execute this program, it prints the max value just fine, however the min value always prints to zero. I continue to scratching my head... Can anyone see what is wrong here? Thanks for looking.

    import java.util.Scanner;
    public class MinMax
    {

    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);
        int [] numbers = new int[5];
        int max = numbers[0]; 
        int min = numbers[0];

        for (int i = 0; i < numbers.length; i++)
        {
            System.out.println("Enter your next number:");
            numbers[i] = kb.nextInt();
            if (numbers[i] > max)
            {
                max = numbers[i];
            }
            if (min > numbers[i])
            {
                min = numbers[i];
            }
        }

        System.out.println("The maximum value in your array is " + max);
        System.out.println("The minimum value in your array is " + min);
    }

}

3 Answers 3

1

The issue is that when the array is declared, the ints in the array are set to 0. Setting the min to numbers[0] would set min to 0. If that's not your min, your code will fail.

In this case, you don't need the array - you could just store whatever the user inputted. That aside, just check whether i==0 and when it does, set min and max to numbers[0]. (If you didn't do the same for max, an array of all negatives would fail.)

Sign up to request clarification or add additional context in comments.

1 Comment

testing this with all negative numbers makes the max 0 and the min = to the actual min in the user input array!
0

It's simple. The min variable is never updated because every time that min > numbers[i] is evaluated returns false. Let's to see an example:

  1. min = 0.0 > numbers[i] = 4.5 -> false
  2. min = 0.0 > numbers[i] = 3.8 -> false
  3. min = 0.0 > numbers[i] = -8.9 -> true, min = -8.9
  4. min = -8.9 > numbers[i] = 7.5 -> false
  5. min = -8.9 < numbers[i] = 5.6 -> false

The value of min is: -8.9

With Java 8 you can get the max and min values easy with lambdas:

max = Arrays.stream(numbers).max().getAsDouble();
min = Arrays.stream(numbers).min().getAsDouble();

Comments

0

As other answers here are saying, the problem is that numbers[0] starts out initialized to 0, so regardless of the numbers the user enters, your code still finds 0 to be the minimum value.

What you need is an extra state to represent "I don't have any minimum value yet". You could use an extra boolean variable to represent this tate, or if you can use the Integer wrapper type, you can use null.

For example:

Integer minimum = null;
Integer maximum = null;

for (int i = 0; i < 5; i++) {
  int number = kb.nextInt();
  if (minimum == null || number < minimum) {
    minimum = number;
  }
  if (maximum == null || number > maximum) {
    maximum = number;
  }
}

System.out.println("minimum: " + minimum);
System.out.println("maximum: " + maximum);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.