0

here is what i have so far. i had to get the avg of 4 real numbers then get the maximum and minimum. i was able to get

import java.util.Scanner;

public class AverageMaxMin {

    public static void main(String[] args) {

                System.out.println("Enter 4 numbers:");
                Scanner input=new Scanner(System.in); 

                    double a,b,c,d;

                    double avg;

                    a=input.nextDouble();
                    b=input.nextDouble();
                    c=input.nextDouble();
                    d=input.nextDouble();
                    avg=(double)(a+b+c+d)/4.0;
                    System.out.println("The Average is: " + avg);

    }
}
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
zeogold
  • 17
  • 6
  • 2
    I would look into, https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#max(double,%20double) the method below deals with getting the minimum. – Matthew Brzezinski Oct 28 '16 at 21:09
  • @pshemo sorry for the shouting. – zeogold Oct 28 '16 at 21:13
  • @MattBrzezinski thanks. ill look into it right now – zeogold Oct 28 '16 at 21:13
  • To get the max you can just do `Math.max(Math.max(a, b), Math.max(c, d));`, to find the minimum you can do similar just replace with the appropriate method. @zeogold – Matthew Brzezinski Oct 28 '16 at 21:20
  • @MattBrzezinski oh okay. this is how i was doing it "if( a > b ){ double max1 = a; double max2 = b; – zeogold Oct 28 '16 at 21:22
  • That works as well, there are many ways of doing this, just doing a greater than / less than comparison works just as well. I only suggested the Math library because you can one line like so, `System.out.println("Max = " + Math.max(Math.max(a, b), Math.max(c, d)) + "\nMin = " + Math.min(Math.min(a, b), Math.min(c, d)));` EDIT: It would be best to do like @Mike Jenkins suggested with iterating, you can add in the values into an array and then loop through to find the maximum and minimum. This would be best because it allows you to expand easily. – Matthew Brzezinski Oct 28 '16 at 21:24
  • @MattBrzezinski thanksill give it a try – zeogold Oct 28 '16 at 23:42

1 Answers1

0

Try looping over the numbers and track the lowest and highest numbers seen.

MikeJ
  • 2,367
  • 3
  • 18
  • 23