简体   繁体   English

将if语句转换为Java中的条件运算符

[英]converting an if statement to a conditional operator in Java

How do I simplify this code with a conditional operator in Java? 如何使用Java中的条件运算符简化此代码? This is from dr. 这是来自博士。 Liang's Introduction to Java. 梁的Java简介。

public class Test {
    public static void main(String[] args) {
        int i = 3;
        int j = 4;
        int k = max(i, j);
        System.out.println("The max int is " + k);
    }

    public static int max(int num1, int num2) {
        int result;

        if (num1 > num2) {
            result = num1;
        }

        else {
            result = num2;
        }

        return result;

    }
}

You can turn things into a one-liner using the conditinal ternary operator: 您可以使用conditinal三元运算符将事物转换为单行:

return (num1 > num2) ? num1 : num2;

But just for the record: it would be better to keep that max method around; 但仅仅是为了记录:保持最大方法更好; simply for the sake of readability. 只是为了便于阅读。 It helps to have that functionality in its own method. 有助于在自己的方法中使用该功能。

But of course: you want to read about java.lang.Math.max() which already provides exactly that functionality (not only for int , but all the other numerical primitive types as well)! 不过,当然:你想了解java.lang.Math.max()它已经提供正是这样的功能(不仅对INT,但所有其它数值基本类型为好)!

So, the real answer is: yes, use a method for that check; 所以,真正的答案是:是的,使用一种方法进行检查; but don't create your own version; 但不要创建自己的版本; use the one that is already coming with Java by default. 使用默认情况下已经使用Java的那个。 In other words: do not re-invent the wheel. 换句话说: 不要重新发明轮子。 Even when it is just a small wheel like this one. 即使它只是像这样的小轮子。

While you could use the conditional operator ? : 虽然你可以使用条件运算符? : ? : to implement this ? :实现这一点

public static int max(int num1, int num2) {
    return num1 > num2 ? num1 : num2;
}

But, I would prefer Math.max(int, int) like 但是,我更喜欢 Math.max(int, int)类的

public static int max(int num1, int num2) {
    return Math.max(num1, num2);
}

You might also choose to use varargs and support an arbitrary number of int (s). 您也可以选择使用varargs并支持任意数量的int Using an IntStream you could do 使用IntStream你可以做到

public static int max(int... nums) {
    return IntStream.of(nums).max().getAsInt();
}

You can use a conditional ternary operator . 您可以使用条件三元运算符

int k = (i > j) ? i : j;

This expression evaluates to: 此表达式的计算结果为:

If i is greather than j , assign i to k . 如果i大于j greather,分配 K。 If not, assign j to k . 如果不是,则将j分配给k

This would be one way to modify your example to use the ternary operator. 这将是修改示例以使用三元运算符的一种方法。

You should generally keep the values in the operator short otherwise they get hard to read. 您通常应该将操作符中的值保持为短,否则将难以阅读。

public class Test {
    public static void main(String[] args) {
        int i = 3;
        int j = 4;
        int k = max(i, j);
        System.out.println("The max int is " + k);
    }

    public static int max(int num1, int num2) {
        int result;

        result = (num1 > num2) ? num1 : num2;
        return result;

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM