我有這段代碼。計數器裏面的斯卡拉理解
for {
country <- getCountryList
city <- getCityListForCountry(country)
person <- getPersonListForCity(person)
} {...}
當我們運行這段代碼,我們需要有這每遞增執行一次循環,循環的主體內的計數器。這個計數器需要顯示每個國家處理的人數。因此,每次我們開始爲新的國家執行循環時,它必須重置爲0。
我試圖
for {
country <- getCountryList
counterPerCountry = 0
city <- getCityListForCountry(country)
person <- getPersonListForCity(city)
} {counterPerCountry = counterPerCountry + 1; ...}
但這說,我試圖重新分配一個值VAL。
所以我試圖
var counterPerCountry = 0
for {
country <- getCountryList
counterPerCountry = 0
city <- getCityListForCountry(country)
person <- getPersonListForCity(city)
} {counterPerCountry = counterPerCountry + 1; ...}
也試過
for {
country <- getCountryList
var counterPerCountry = 0
city <- getCityListForCountry(country)
person <- getPersonListForCity(city)
} {counterPerCountry = counterPerCountry + 1; ...}
循環中計數器的用途是什麼?你能不能做一些像'counter < - 1 getList1.length'? –
好的。將list1視爲國家。列表2爲城市,列表3爲人。櫃檯需要計算每個國家處理的人數。所以每次我們選擇一個新的國家(從名單1)我們需要重置櫃檯。每次循環迭代時,計數器都必須遞增,因爲我們使用計數器顯示處理的狀態。 –
好的,計數器也需要從1到1。如果是這樣,我認爲我上面的評論提供了所需的語法... –