在我開始之前,我已經檢查出Handle badarg in Erlang 但是我在未定義的檢查中仍然沒有成功,所以我刪除了它們。Erlang「badarg」,不知道如何處理
我建立一個假的銀行過程,並在客戶端執行的平衡查詢支票到銀行的過程中,程序退出時,他說:
Error in process <0.373.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,27}]}]}
=ERROR REPORT==== 26-Oct-2016::13:34:57 ===
Error in process <0.379.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,27}]}]}
=ERROR REPORT==== 26-Oct-2016::13:34:57 ===
Error in process <0.375.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,27}]}]}
=ERROR REPORT==== 26-Oct-2016::13:34:58 ===
Error in process <0.377.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,27}]}]}
<0.378.0> Balance request: 54>
=ERROR REPORT==== 26-Oct-2016::13:34:58 ===
Error in process <0.378.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,39}]}]}
<0.372.0> Balance request: 54>
=ERROR REPORT==== 26-Oct-2016::13:34:58 ===
Error in process <0.372.0> with exit value:
{badarg,[{project4,client,3,
[{file,"/Users/owner/Desktop/bank.erl"},
{line,39}]}]}
用於模擬的代碼是在這裏:
-module(bankSim).
-export([start/0, negativeOrPositive/0, sleep/1, generate_rand_int_list/3, generate_rand/2, client/3, clientSpawn/2, bank/2]).
negativeOrPositive() ->
M = rand:uniform(),
if
M =< 0.5 ->
1;
true ->
-1
end.
sleep(T) ->
receive
after T -> ok
end.
generate_rand_int_list(N,StartVal,Lim) ->
lists:map(fun (_) -> (rand:uniform(Lim-StartVal) + StartVal)* negativeOrPositive() end, lists:seq(1,N)).
generate_rand(StartVal, Lim) ->
rand:uniform(Lim-StartVal) + StartVal.
client([], _ , BankID) ->
BankID ! {goodbye};
client([H|T], Count, BankID) ->
BankID ! {transaction, self(), H},
receive
{Amount, Balance, Success} ->
io:format("Client: ~w, Amount requested: ~w, Bank Balance: ~w, Transaction successful ~w ~n",[self(), Amount, Balance, Success]);
{ X } ->
io:format("The balance is ~w ~n", [X])
end,
sleep(generate_rand(500, 1500)),
Mod = Count rem 5,
if
Mod == 0 ->
io:format("~w Balance request: ",[ self() ]),
BankID! {balance, self()};
true ->
ok
end,
client(T, Count + 1, BankID).
clientSpawn(0, _) ->
io:format("Finished spawning clients ~n",[]);
clientSpawn(N, BankID) ->
spawn(bankSim, client, [ generate_rand_int_list(generate_rand(10, 20), 0, 100) , 1, BankID]),
clientSpawn(N-1, BankID).
bank(Balance, 0) ->
io:format("Banking simulation ended with a final balance of ~w ~n", [Balance]),
io:format("simulation completed ~n", []);
bank(Balance, NumClients) ->
receive
{balance, Client} ->
Client ! {Balance};
{transaction, Client, Amount} ->
NewBalance = Balance + Amount,
if
NewBalance =< 0 ->
Client ! {Amount, NewBalance, no},
bank(Balance, NumClients);
NewBalance > 0 ->
Client ! {Amount, NewBalance, yes},
bank(NewBalance, NumClients);
true ->
io:format("This will never be printed")
end;
goodbye ->
NewNumClients = NumClients - 1,
bank(Balance, NewNumClients)
end.
start()->
N = generate_rand(2,10),
register(bank ,spawn(bankSim, bank, [generate_rand(2000,3000), N])),
clientSpawn(N,bank).
任何幫助,將不勝感激。
看起來您忘記了遞歸調用「{balance,Client}」的消息嗎? (57-58行。) – Dogbert
這似乎奏效了,我一直在盯着它,這麼長時間以來,我可能一直在爲這樣一個簡單的錯誤着迷。 –