繁体   English   中英

"三元运算符中的多个条件"

[英]Multiple conditions in ternary operators

首先,问题是“编写一个 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.");
    }
}

尝试

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

您还可以删除括号:

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

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

首先弄清楚如何使用单个三元运算符编写 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);
    }
}

当您确实不需要时,您正在测试 z。 你的三元运算符必须是 cond 形式? 如果真:如果假;

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

条件 1? ifTrue1:cond2? 如果 True2 : ifFalse2;

如果你明白这一点,请不要往下看。 如果您仍然需要帮助,请往下看。

我还包含了一个更清晰的不嵌套它们的版本(假设您不需要嵌套它们。我当然希望您的作业不需要您嵌套它们,因为那太丑了!)

.

.

这是我想出的:

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.");
    }
}

我知道已经晚了。 仅供参考,这也适用:

int smallestNum = (x<y ? x : (x=y)) < z ? x : z
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);
}

我的解决方案:

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.");
}

我的贡献 ...

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 ) );
}

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

(z<y && z<x) ? z : some_value;
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);
    

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

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

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

您错过了 z var minimumNum = (x<y && x<z) 之后的值? x : (y<x && y<z) ? y : (z<y && z<x) ? z:0; 试试这个,它有效

public class questionNine{
  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);
    System.out.println(smallestNum);
  }
}

最好的方法是使用 if 和 else 语句做一个例子,然后对其应用三元运算符(q

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

暂无
暂无

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

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