public class Demo01 { public static void main(String[] args) { //字符串连接符 + String int a=20; int b=10; System.out.println(""+a+b); System.out.println(a+b+""); //三元运算符 //x ? y : z //如果x==true,则结果为y,否则结果为z int score = 80; String type = sco…
一.三元运算符 三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值 格式:[on_true] if [expression] else [on_false] res = 值1 if 条件 else 值2 1.举例说明: a =1 b = 2 c= a if a>1 else b #如果a大于1的话,c=a,否则c=b 如果不用三元运算符,就用if-else条件判断,如下: a = 1 b = 2 if a >1: c = a else: c = b 2.三元运算符也可以用在列表和…
package 课上练习; public class 三元运算符 { //用法: 数据类型 变量 = 布尔表达式? 条件满足设置内容:条件不满足设置内容 : public static void main(String[] args) { ; ; int result = x > y ? x : y ; System.out.println( result ) ; x = ; result = x > y ? x : y ; System.out.println( result ) ; } }…
在c.php里面,都有三元运算符,如: a = b?c:d 意思是 b 的运算结果如果是True,那么整个表达式a就等于c,反之如果b的运算结果是False,那么a就等于d. 这样写起来简洁又高效,否则就得写个一if else块,起码3 4 行. 但是python一开始却没有三元运算符,后来由于广大程序猿们的渴求,吉多在python2.5加入了这样一个语法表示: a=b if c else d 表示:如果c的值是True,那么a就等于b,否则如果c的值是False,那么a就等于d…