gpt4 book ai didi

java - 如果两个选项之一不为空,如何使变量选择

转载 作者:行者123 更新时间:2023-12-05 00:13:28 24 4
gpt4 key购买 nike

在我知道的其他语言中,我可以做到以下几点。但是我在 Java 中遇到了问题,无法理解为什么我不能这样做。我的错误是:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:     The operator || is undefined for the argument type(s) java.lang.String, java.lang.String

My code is:

public class Tester {

public static void main(String[] args)
{
String one = null;
String two = "two";
String three = one || two;

System.out.println(three);
}

}

最佳答案

使用三元运算符:

String three = (one != null) ? one : two;

关于java - 如果两个选项之一不为空,如何使变量选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24937437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com