我在“Shop”课程中有以下代码:
public Item sellItem()
{
displayItems();
int indexID = Shop.getInput();
if (indexID <= -1 && indexID >= wares.length)
{
System.out.println("Null"); // For testing purposes
return null;
}
else
{
return wares[indexID];
}
}
我想知道如何编写if
语句,检查此方法是否在我的主类中为null
循环返回while
:
int shopInput = scan.nextInt();
if(shopInput >= 1 && shopInput <= allShops.length)
{
boolean leaveShop = true;
while(leaveShop)
{
allShops[shopInput - 1].sellItem();
if(???????? == null);
{
System.out.println("still null"); // For testing purposes
leaveShop = false;
}
}
}
答案 0 :(得分:0)
Item item = allShops[shopInput - 1].sellItem();
if (item == null) {
System.out.println("still null"); // For testing purposes
...
}
(请注意,我从;
行的末尾删除了if
答案 1 :(得分:0)
使用此选项检查是否为空。
if (allShops[shopInput - 1].sellItem() == null) {}
编辑:删除了不必要的双重否定