三目运算除了用于赋值运算中还可以用于字符串吗?
三目运算类似
if cond:
do a
else:
do b
所以也可以用于字符串操作
理论上可以,三目运算符就是一个if else结构
能作为条件就可以运算
三目运算类似
if cond:
do a
else:
do b
所以也可以用于字符串操作
public static void main(String[] args) {
String str1 = "asd";
String str2 = str1.equals("asd") ? "bcd" : "def";
String str3 = str1.equals("asdf") ? "bcd" : "def";
System.out.println(str2);
System.out.println(str3);
}