我學習Java,我有一個問題ArrayList和RandomGenerator。我有一個名為的對象catalogue,該對象具有從另一個名為的類創建的對象的數組列表item。我需要一種方法,catalogue其中返回item列表中一個對象的所有信息。該item隨意選擇的需求。import java.util.ArrayList;import java.util.Random;public class Catalogue{ private Random randomGenerator; private ArrayList<Item> catalogue; public Catalogue () { catalogue = new ArrayList<Item>(); } public Item anyItem() { int index = randomGenerator.nextInt(catalogue.size()); return catalogue.get(index); System.out.println("Managers choice this week" + anyItem + "our recommendation to you"); }當我嘗試編譯時,出現錯誤,指出System.out.println行說..'找不到符號變量anyItem'
3 回答

海綿寶寶撒
TA貢獻1809條經驗 獲得超8個贊
您必須system.out.println從下方刪除消息,如下所示return:
public Item anyItem()
{
randomGenerator = new Random();
int index = randomGenerator.nextInt(catalogue.size());
Item it = catalogue.get(index);
System.out.println("Managers choice this week" + it + "our recommendation to you");
return it;
}
該return語句基本上說該函數現在將結束。超出return語句范圍的任何內容都將導致您遇到的行為
添加回答
舉報
0/150
提交
取消