Home > Net >  How can I use conditional operators to get the highest and lowest value among the five variables?
How can I use conditional operators to get the highest and lowest value among the five variables?

Time:10-03

So far, I've only figured out how to get the highest and lowest values from the three variables using the conditional operator. Basically, this is what it looks like on the three variables:

int highnum = n1>n2?n1:n2; highnum = n3>highnum?n3:highnum;

Same concept goes to finding the lowest which is just replacing (>) with (<). When it comes to five variables, I still can't quite know how to write them.  

CodePudding user response:

Starting Point

Let's assume that the variables are

  • n1
  • n2
  • n3
  • n4
  • n5

Doing it with ternary operator:

max = (max = (max = (max = (n1 > n2 ? n1 : n2)) > n3 ? max : n3) > n4 ? max : n4) > n5 ? max : n5;

This is incredibly messy (there might be a typo in the untested code above).

Doing it with conditionals

max = n1;
if (n2 > max) max = n2;
if (n3 > max) max = n3;
if (n4 > max) max = n4;
if (n5 > max) max = n5;

Doing it with arrays

int myArray = new int[] {n1, n2, n3, n4, n5};
int max = n1;
for (index = 1; index < myArray.length; index  ) {
    if (max < myArray[index]) max = myArray[index];
}

Doing it with Math.max

int max = Math.max(Math.max(Math.max(Math.max(n1, n2), n3), n4), n5)

CodePudding user response:

You can do so by putting the second condition in the else part (i.e. the part which after :).

Demo:

public class Main {
    public static void main(String[] args) {
        int x = 15, y = 5, z = 10;
        int max = x > y && x > z ? x : y > x && y > z ? y : z;
        int min = x < y && x < z ? x : y < x && y < z ? y : z;

        System.out.println("Max: "   max   ", Min: "   min);
    }
}

Output:

Max: 15, Min: 5

If you are prohibited from using &&, you can do it as follows:

int max = x > y ? (x > z ? x : z) : y > z ? y : z;
int min = x < y ? (x < z ? x : z) : y < z ? y : z;

CodePudding user response:

The optimal way is to use a loop/ define you own max function with Varargs¹.

It's very nasty to do it using only conditional operators, if you need it for an exercise you can do this:

int highnum = ((n1>n2 ? n1 : n2) > (n3>n4 ? n3 : n4) ? (n1>n2 ? n1:n2) : (n3>n4?n3:n4)) > n5 ? ((n1>n2 ? n1 : n2) > (n3>n4 ? n3 : n4) ? (n1>n2 ? n1:n2) : (n3>n4?n3:n4)) : n5;

Or you can define a method as following:

public static int max(int a,int b){
    return a>b?a:b;
}

and then do something like this:

int highnum = max(max(max(n1,n2),max(n4,n3)),n5);

1: Varargs method:

public static int max(int... nums){
    int max = nums[0];
    for (int num : nums) if(num>max) max=num;
    return max;
}

int highnum = max(n1,n2,n3,n4,n5);

CodePudding user response:

Anyway, don't feel it makes sense to do it with ternary operator for 5 variables.(Unless you want to train your brain).

Below just supplement on how to do it using Stream.

Stream approach

Make use of IntStream and corresponding summaryStatistics method:

import java.util.IntSummaryStatistics;
import java.util.stream.IntStream;

public class IntStatistic {
    public static void main(String[] args) {
        int n1 = 9;
        int n2 = 7;
        int n3 = 5;
        int n4 = 3;
        int n5 = 1;
        IntSummaryStatistics statistic = IntStream.of(n1, n2, n3, n4, n5).summaryStatistics();
        System.out.println("min "   statistic.getMin());
        System.out.println("max "   statistic.getMax());
    }
}
  •  Tags:  
  • java
  • Related