小编Mik*_*ike的帖子

三元运算符中的多个条件

首先,问题是"编写一个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.");
    }
}
Run Code Online (Sandbox Code Playgroud)

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

java ternary operator-keyword

16
推荐指数
4
解决办法
8万
查看次数

标签 统计

java ×1

operator-keyword ×1

ternary ×1