【发布时间】:2021-05-11 00:58:40
【问题描述】:
我正在尝试为此项目使用异常块,如果子字符串与“标题”或“作者”不匹配,则需要引发异常。我想使用 if (condition) is false 语句来抛出我的异常,但我知道对于字符串我无法与 != 进行比较,因为运算符比较的是地址而不是内容。
如果 (condition1 || condition2 ) is false { 抛出异常}
public String getLongest(String property) throws IllegalNovelPropertyException{ String longest = ""; if (property.equalsIgnoreCase("author" ) || property.equalsIgnoreCase("title" ) is false); { throw new IllegalNovelPropertyException("Bad property. Substring must be title or author."); }
【问题讨论】:
-
if (!(condition1 || condition2 ))