我有一類「商店」下面的代碼:我應該如何調用一個類來檢查它是否返回null?
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;
}
}
}
你嘗試如果(allShops [shopInput - 1] .sellItem()== NULL){....}? – 2013-03-09 05:59:21