Question

Write a program that accepts as many integers the user wants to input. Determine the highest and lowest numbers and subtract all other numbers from the highest number. I can't get the difference. I know there's missing here. please help me.

import java.util.*;
//Misty Stewart
public class HighestLowestDifference2324 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        double maxValue = Double.MIN_VALUE;
        double minValue = Double.MAX_VALUE;
        double minValue1 = Double.MAX_VALUE;
        double minValue2 = Double.MAX_VALUE;

        int count = 0;
        double difference = 0;

        System.out.println("Enter numbers. Terminate with Q.");
        while (input.hasNextDouble()) {

            double Value = input.nextDouble();

            if (Value > maxValue) {
                maxValue = Value;
            }
            if (Value < minValue) {
                minValue = Value;
            }
            if (Value < minValue) {
                minValue1 = Value;
            }
            if (minValue == minValue1) {
                minValue = Value;
            }
            if (Value < minValue1) {
                minValue2 = Value;
            }
            if (minValue1 == minValue2) {
                minValue1 = Value;
            }
        }
        count++;
        difference = maxValue - minValue;
        {
            if (count > 0) {
                System.out.println("Highest no. is: " + maxValue);
                System.out.println("Lowest no. is: " + minValue);
                System.out.println("The difference from the highest is: \n" + difference);
            } else {
                System.out.println("Have a good day!");
            }
        }
    }
}
Was it helpful?

Solution 2

How about this:

    double maxValue = Double.MIN_VALUE;
    double minValue = Double.MAX_VALUE;
    List<Double> allValues = new ArrayList<Double>();
    while (input.hasNextDouble()) {
        double value = input.nextDouble();
        maxValue = Math.max(maxValue, value);
        minValue = Math.min(minValue, value);

        allValues.add(value);
    }

    System.out.println("Highest no. is: " + maxValue);
    System.out.println("Lowest no. is: " + minValue);
    System.out.println("The difference from the highest is:" + (maxValue - minValue));
    for (Double value : allValues) {
        System.out.println("The difference from the highest is:" + (maxValue - value));
    }

OTHER TIPS

You need to store the values entered by the user in an array. And then once you have the maxVAlue, iterate over the array with a loop and calculate and display the difference.

while (input.hasNextDouble()) {
 num = input.nextDouble();
 if ( num > max ) max = num;
 if ( num < min ) min = num;
}

difference = max -min;

above code can be used to get the highest , lowest , and their difference from list of numbers

You should probably store all the input numbers in a Collection, for example an ArrayList, and do your analysis afterwards:

ArrayList<Double> list = new ArrayList<Double>();
while (input.hasNextDouble()) {
  double value = input.nextDouble();

  // logic for determining largest & smallest

  list.add(value);
}

And afterwards iterate over the ArrayList to get your desired output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top