使用三元運算子查詢三個數字中最大數字的 Java 程式


條件運算子也稱為三元運算子。該運算子包含三個運算元,用於求值布林表示式。該運算子的目標是確定應將哪個值分配給變數。運算子寫為 -

variable x = (expression) ? value if true : value if false

示例

即時演示

public class LargestOf3Nums_TernaryOperator {
   public static void main(String args[]) {
      int a, b, c, temp, result;
      a = 10;
      b = 20;
      c = 30;

      temp = a < b ? a:b;
      result = c < temp ? c:temp;
      System.out.println("Largest number is ::"+result);
   }
}

輸出

Largest number is ::30




更新日期: 13-Mar-2020

560 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始使用
廣告
© . All rights reserved.