• 3.27条件运算符


    3.27条件运算符

    总览

    解读:

    • x是指一个布尔表达式

    • y是一个值,z是另一个值

    • y和z不一定是字符串,也可以是数字也可以是式子等等

    过程:

    1. 先计算布尔表达式x的值

    2. 判断布尔表达式的值是true还是false

    3. 如果为true则运算结果为表达式y的值

    4. 如果为false则运算结果为表达式z的值

    可以表示一些简单的if...else

    实例:

    /**
    * 条件运算符(三元运算符)
    * @author Lucifer
    */
    public class TestOperatorNo8 {
       public static void main(String argument[]){
           int source = 80;
           int flag = -100;
           String type = source < 60? "及格":"不及格";
           //上面的三位条件运算符等价于if...else
           System.out.println(type);

           if(source<60){
               System.out.println("不及格");
          }else {
               System.out.println("及格");
          }

           System.out.println(flag > 0?1 : (flag==0?0 : -1));

      }
    }

     

    It's a lonely road!!!
  • 相关阅读:
    【NOIP2013】花匠
    【DP合集】tree-knapsack
    【DP合集】m-knapsack
    【DP合集】背包 bound
    【DP合集】合并 union
    【DP合集】棋盘 chess
    BZOJ1026 [SCOI2009]windy数
    最长上升子序列 LIS nlogn
    再谈线性基
    数论问题算法模板
  • 原文地址:https://www.cnblogs.com/JunkingBoy/p/14587219.html
Copyright © 2020-2023  润新知