1
我正在實施一個停車場,有2個入口門和1個可以離開公園的停車場。對我來說,一切都看起來不錯,但我得到這樣的錯誤過程創建問題
Error in process <0.84.0> with exit value: {badarg,[{parking,car,2},{random,uniform,0}]}
我的代碼是:
-module (parking2).
-export ([start/3]).
-export ([car/2, parkingLoop/1]).
carsInit(0, _Iterations) ->
ok;
carsInit(Number, Iterations) ->
spawn(parking, car, [Number, Iterations]),
carsInit(Number - 1, Iterations).
car(_ID, 0) ->
ok;
car(ID, Iterations) ->
Gate = random:uniform(2),
parking ! {enter, self()},
receive
error ->
io:format("Car ~B ncanot enter - there is no free place.~n", [ID]),
Time = random:uniform(1000),
timer:sleep(Time),
car(ID, Iterations);
ok ->
io:format("Car ~B entered through the ~B gate~n", [ID, Gate])
end,
StopTime = random:uniform(500) + 500,
timer:sleep(StopTime),
parking ! {leave, self(), ID},
FreerideTime = random:uniform(1000) + 500,
timer:sleep(FreerideTime),
car(ID, Iterations - 1).
parkingInit(Slots) ->
spawn(parking, parkingLoop, [Slots]).
parkingLoop(Slots) ->
receive
{enter, Pid} ->
if Slots =:= 0 ->
Pid ! error
end,
Pid ! ok,
parkingLoop(Slots - 1);
{leave, Pid, ID} ->
io:format("Car ~B left the car park.", [ID]),
parkingLoop(Slots + 1);
stop ->
ok
end.
start(Cars, Slots, Iterations) ->
parkingInit(Slots),
carsInit(Cars, Iterations).
五月人幫助我嗎?我學了Erlang幾天,不知道,這裏有什麼問題。
由於提前, 拉狄克
謝謝你非常。 – Radek 2011-04-01 14:59:36
對不起,我問,但如何做到這一點? – Radek 2011-04-04 21:19:03
啊,沒問題! :-)只需點擊答案左側的答案選項下方的綠色複選框(歡迎來到Stack Overflow!) – 2011-04-05 08:28:47