三元运算符中的多个条件

22

首先,问题是“使用三元运算符编写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.");
    }
}

我尝试在三元运算符中使用多个条件,但是不起作用。 我缺席了几天,所以我不太确定该怎么做,我的老师的电话没有开机。 有人能帮忙吗?


尝试一下,但是最后一个测试用例改为“:z”。 - Aaron Anodide
根据我的经验,仅仅因为你“能够”编写链接的三元语句,并不意味着你“应该”这样做。此外,你目前所拥有的代码非常复杂,你只需要将变量彼此进行一次比较即可:int smallest = min(min(x, y), z) - Juliet
Math.min使用三元运算符。最简单的方法是静态导入此方法并编写smalledNum = min(x, min(y, z)) - Peter Lawrey
考虑修改作业,以应对教授将“三”作为变量的可能情况。这样做很可能会让你获得额外的学分。 - Jeff Holt
14个回答

36

尝试一下

int min = x < y ? (x < z ? x : z) : (y < z ? y : z);
你也可以去除括号:
int min = x < y ? x < z ? x : z : y < z ? y : z;

39
没有括号的版本看起来像是参加混淆C语言比赛的作品。 - Jakub Zaverka
为什么添加额外的括号会出现错误:意外类型 int min = x < y ? ((x < z) ? x : z) : ((y < z) ? y : z); - sofs1

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

5
你正在测试 z,但实际上不需要这样做。你的三元运算符必须是这种形式:cond ? ifTrue : ifFalse;
所以如果你有多个条件,你可以这样写:
cond1? ifTrue1 : cond2? if 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.");
    }
}

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

我喜欢这个实现。让库来完成工作。 - loadedion

2

我知道现在有点晚了,但是这也可以起作用:

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

它正在改变数据,但我可以看到这个工作。 - Mike Warren

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

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

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

在使用参数 (3, 2, 1) 调用时返回 2。 - fthdgn
@fthdgn ~ 很好的发现...已修复。 - Edward J Beckett

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

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接