[英]How can I use conditional operators to get the highest and lowest value among the five variables?
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.说到五个变量,我还是不太清楚怎么写。
You can do so by putting the second condition in the else
part (ie the part which after :
).您可以通过将第二个条件放在else
部分(即:
之后的部分)来实现。
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 : 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;
Let's assume that the variables are让我们假设变量是
n1
n2
n3
n4
n5
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).这非常混乱(上面未经测试的代码可能有错字)。
max = n1;
if (n2 > max) max = n2;
if (n3 > max) max = n3;
if (n4 > max) max = n4;
if (n5 > max) max = n5;
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];
}
int max = Math.max(Math.max(Math.max(Math.max(n1, n2), n3), n4), n5)
As @Edwin Dalorzo pointed out in the comment section, IntStream
is also a possible approach.正如@Edwin Dalorzo 在评论部分指出的那样, IntStream
也是一种可能的方法。 This is the first time I have heard of IntStream
, so the following example may be incorrect.这是我第一次听说IntStream
,所以下面的例子可能不正确。 If so, I graciously thank any issue pointed out about it:如果是这样,我非常感谢指出的任何问题:
IntStream.of(n1,n2,n3,n4,n5).reduce(max,Math::max)
Explanation (as far as I understand):解释(据我所知):
reduce
, so, under the hood all elements are considered and the operator is applied ( https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#reduce-int-java.util.function.IntBinaryOperator- )我们称之为reduce
,因此,在引擎盖下考虑所有元素并应用运算符( https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#reduce-int -java.util.function.IntBinaryOperator- )max
, a variable as first parameter to reduce
, to specify that we compute the maximum我们将max
作为第一个参数传递给reduce
,以指定我们计算最大值Math::max
as the operator, so reduce
will compute the maximum.我们将Math::max
作为运算符传递,因此reduce
将计算最大值。 The optimal way is to use a loop/ define you own max
function with Varargs¹ .最佳方法是使用循环/使用 Varargs¹ 定义您拥有的max
function 。
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: 1:可变参数方法:
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);
Anyway, don't feel it makes sense to do it with ternary operator for 5 variables.(Unless you want to train your brain).无论如何,不要觉得用三元运算符来处理 5 个变量是没有意义的。(除非你想训练你的大脑)。
Below just supplement on how to do it using Stream.下面仅补充如何使用 Stream 进行操作。
Make use of IntStream
and corresponding summaryStatistics
method:使用IntStream
和相应的summaryStatistics
方法:
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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.