【问题标题】:Multiple conditions in ternary operators三元运算符中的多个条件
【发布时间】:2011-06-26 13:52:23
【问题描述】:

首先,问题是“编写一个 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”
  • 至少在我的经验中,仅仅因为你可以编写链式三元语句,并不意味着你应该。此外,到目前为止,您所拥有的内容非常复杂,您只需将变量相互比较一次:int smallest = min(min(x, y), z).
  • Math.min 使用三元运算符。最简单的就是静态导入这个方法,写smalledNum = min(x, min(y, z))
  • 当教授将“三”作为变量时,考虑修改可能事件的赋值。这可能会为您带来额外的功劳。

标签: java operator-keyword ternary


【解决方案1】:

试试

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

你也可以去掉括号:

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

【讨论】:

  • 无括号版本看起来像是 Obfuscated-C 竞赛的参赛作品。
  • 为什么添加额外的括号会出现错误:意外类型int min = x &lt; y ? ((x &lt; z) ? x : z) : ((y &lt; z) ? y : z);
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    当你真的不需要时,你正在测试 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.");
        }
    }
    

    【讨论】:

      【解决方案4】:
      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);
      }
      

      【讨论】:

      • 我喜欢这个实现。让图书馆做这项工作
      【解决方案5】:

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

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

      【讨论】:

      • 虽然它正在改变数据,但我可以看到这个工作
      【解决方案6】:

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

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

      【讨论】:

        【解决方案7】:

        我的解决方案:

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

        【讨论】:

          【解决方案8】:

          我的贡献……

          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 ~ Good Catch... 已修复。
          【解决方案9】:
          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);
              
          

          【讨论】:

            【解决方案10】:

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

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

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

            【讨论】:

              【解决方案11】:

              你错过了 z var 之后的值 最小数 = (x

              【讨论】:

                【解决方案12】:
                int min = (x<y)?((x<z)?x:z):((y<z)?y:z);
                

                【讨论】:

                  【解决方案13】:

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

                  【讨论】:

                  • 请在您的回答中添加更多细节
                  猜你喜欢
                  • 2012-09-14
                  • 2013-01-14
                  • 2013-06-06
                  • 2018-03-06
                  • 2015-08-29
                  • 2021-11-20
                  • 2020-05-18
                  • 2016-11-02
                  • 2019-01-22
                  相关资源
                  最近更新 更多