三元运算符中的多个条件

时间:2011-02-13 20:26:09

标签: java operator-keyword ternary

首先,问题是“编写一个Java程序,使用三元运算符找到三个最小的数字。”

这是我的代码:

class questionNine
{
    public static void main(String args[])
    {
        int x = 1, y = 2, z = 3;
        int smallestNum;

        smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y : (z<y && z<x) ? z;
        System.out.println(smallestNum + " is the smallest of the three numbers.");
    }
}

我尝试在三元运算符中使用多个条件,但这不起作用。几天我不在,所以我不确定该做什么,老师的电话已关闭。有什么帮助吗?

13 个答案:

答案 0 :(得分:25)

尝试

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

您也可以删除括号:

int min = x < y ? x < z ? x : z : y < z ? y : z;

答案 1 :(得分:14)

由于这是作业,我不只是给你答案,而是一个算法,你可以自己解决。

首先研究如何使用单个三元运算符编写min(x,y)。

完成后,将min(x,y,z)的以下代码更改为使用三元运算符,然后在代码中替换上一步中得到的min(x,y)。

int min(x, y, z) {
    if (x <= y) {
        return min(x, z);
    } else {
        return min(y, z);
    }
}

答案 2 :(得分:4)

当你真的不需要时,你正在测试z。您的三元运算符必须是cond? ifTrue:ifFalse;

所以如果你有多个条件,你就有了这个:

COND1? ifTrue1:cond2?如果True2:ifFalse2;

如果您理解这一点,请不要在下面看。如果您仍需要帮助,请查看以下内容。

我还包括一个没有嵌套它的版本更清晰(假设你不需要嵌套它们。我肯定希望你的作业不需要你嵌套它们因为那太丑了!)< / p>

这是我想出的:

class QuestionNine
{
    public static void main(String args[])
    {
        smallest(1,2,3);
        smallest(4,3,2);
        smallest(1,1,1);
        smallest(5,4,5);
        smallest(0,0,1);
    }

    public static void smallest(int x, int y, int z) {
        // bugfix, thanks Mark! 
        //int smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y :  z;
        int smallestNum = (x<=y && x<=z) ? x : (y<=x && y<=z) ? y :  z;
        System.out.println(smallestNum + " is the smallest of the three numbers.");
    }

    public static void smallest2(int x, int y, int z) {
       int smallest = x < y ? x : y; // if they are equal, it doesn't matter which
       smallest = z < smallest ? z : smallest;
       System.out.println(smallest + " is the smallest of the three numbers.");
    }
}

答案 3 :(得分:2)

public static int min(int x, int y, int z) {
    return x<y?Math.min(x, z):Math.min(y, z);           
}

public static void main(String[] args) {
    int smallestNum = min(10, 12, 15);
    System.out.println(smallestNum);
}

答案 4 :(得分:2)

我知道它已经很晚了。仅供参考,这也有效:

int smallestNum = (x<y ? x : (x=y)) < z ? x : z

答案 5 :(得分:1)

最后一部分:(z<y && z<x) ? z缺少':':

(z<y && z<x) ? z : some_value;

答案 6 :(得分:1)

我的解决方案:

public static void main(String args[])
{
    int x = 1, y = 2, z = 3;
    int smallestNum = (x < y && x < z) ? x :
        (y < x && y < z) ? y :
        (z < y && z < x) ? z:y;
    System.out.println(smallestNum + " is the smallest of the three numbers.");
}

答案 7 :(得分:1)

我的贡献......

public static int getSmallestNumber( int x, int y, int z) {

     return x < y && x < z ? x : y < x && y < z ? y : z;
}

public static void main ( String ... args ) {

    System.out.println( getSmallestNumber( 123, 234, 345 ) );
}

答案 8 :(得分:1)

public static void main(String args[]) {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the number and check heigest number of three number:-");
    int a=sc.nextInt();
    int b=sc.nextInt();
    int c=sc.nextInt();
    int max;
    max=(a>b&&a>c)?a:(b>a&&b>c)?b:(c>a&&c>b)?c:(a==b&&b==c)?a:0;
    
    System.out.println("the heigest number is:"+max);
    

答案 9 :(得分:0)

这个答案迟了七年,所以我只会给你代码:

int smallestNumber = (x > y) ? 
            ((y > z) ? z : y) : 
            ((x > z) ? z : x);

缩进应该解释代码,这只是一个三元数,用于评估初始条件x > y上的其他三元组; / *如果该条件为真,则评估第一个三元组,否则评估第二个三元组。 * /

答案 10 :(得分:0)

您错过了z var之后的值 minimumNum =(x

答案 11 :(得分:-1)

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

答案 12 :(得分:-1)

最好的方法是使用if和else语句创建一个示例,然后在其上应用三元运算符  (Q