Erlang 计算算术表达式时出错

Erlang 计算算术表达式时出错,erlang,erlang-shell,Erlang,Erlang Shell,我是一名Erlang初学者,正在尝试制作一个简单的命令行应用程序,用户输入地板的宽度和高度、每平方英尺地板的成本,然后返回一个价格。本质上,我只是接受三个整数值并返回乘积 23> c(costcalc). {ok,costcalc} 24> costcalc:start(). Calculate the cost of flooring based on width, height, and cost per square foot. Width in feet: 5 Height

我是一名Erlang初学者,正在尝试制作一个简单的命令行应用程序,用户输入地板的宽度和高度、每平方英尺地板的成本,然后返回一个价格。本质上,我只是接受三个整数值并返回乘积

23> c(costcalc).
{ok,costcalc}
24> costcalc:start().
Calculate the cost of flooring based on width, height, and cost per square foot.

Width in feet: 5
Height in feet: 5
Cost in dollars per square foot: $4
** exception error: an error occurred when evaluating an arithmetic expression in function  costcalc:start/0 (costcalc.erl, line 23)
以下是我正在使用的代码:

start() ->
  io:format("Calculate the cost of flooring based on width, height, and cost per square foot.\n"),
  W = string:to_integer(io:get_line("Width in feet: ")),
  H = string:to_integer(io:get_line("Height in feet: ")),
  C = string:to_integer(io:get_line("Cost in dollars per square foot: $")),
  Cost = W * H * C,
  io:fwrite(Cost).
第23行是
Cost=W*H*C,
应该是100。当我在shell中直接运行
5*5*4.
时,它会毫无问题地进行计算。我还应该注意,无论我是否使用string:to_integer(),都会发生这种情况,我想我可以不用它


我遗漏了什么?

字符串的问题是:对于整数,它返回2个值!你应该像这样使用它们

start() ->
    io:format("Calculate the cost of flooring based on width, height, and cost per square foot.\n"),
    {W,_} = string:to_integer(io:get_line("Width in feet: ")),
    {H,_} = string:to_integer(io:get_line("Height in feet: ")),
    {C,_} = string:to_integer(io:get_line("Cost in dollars per square foot: $ ")),
    Cost = (W * H) * C,
    io:fwrite("~p~n",[Cost]).
第二个值是字符串的其余部分

to_integer(String)->{Int,Rest}{error,Reason}


祝您好运

您遇到的问题是字符串:对于整数,它返回2个值!你应该像这样使用它们

start() ->
    io:format("Calculate the cost of flooring based on width, height, and cost per square foot.\n"),
    {W,_} = string:to_integer(io:get_line("Width in feet: ")),
    {H,_} = string:to_integer(io:get_line("Height in feet: ")),
    {C,_} = string:to_integer(io:get_line("Cost in dollars per square foot: $ ")),
    Cost = (W * H) * C,
    io:fwrite("~p~n",[Cost]).
第二个值是字符串的其余部分

to_integer(String)->{Int,Rest}{error,Reason}


祝你好运,

正如@Khashayar所提到的,代码中的问题是返回一对(一个包含两个元素的元组),而整数是第一个元素

但是,不应使用此功能。Erlang中的字符串只是一个整数列表,您要使用的是。这是将字符串转换为整数的常用方法

如果您使用了,就可以避免@Khashayar代码中第二个pair元素与任何匹配的错误。实际上,您可以输入以下内容:

Calculate the cost of flooring based on width, height, and cost per square foot. 
Width in feet: 1.9
Height in feet: 1.9
Cost in dollars per square foot: $ 4.9
4
1.9*1.9*4.9
实际上等于
17.689


不幸的是,没有返回整数或浮点的
list_to_number/1
函数。最常见的处理方法是使用
list\u to\u float/1执行try/catch操作,然后返回到
list\u to\u integer/1
。或者,您可以使用或不引发异常(如上所述,使用被认为是一种不好的做法)。

正如@Khashayar所提到的,代码中的问题是返回一对(一个包含两个元素的元组),而整数是第一个元素

但是,不应使用此功能。Erlang中的字符串只是一个整数列表,您要使用的是。这是将字符串转换为整数的常用方法

如果您使用了,就可以避免@Khashayar代码中第二个pair元素与任何匹配的错误。实际上,您可以输入以下内容:

Calculate the cost of flooring based on width, height, and cost per square foot. 
Width in feet: 1.9
Height in feet: 1.9
Cost in dollars per square foot: $ 4.9
4
1.9*1.9*4.9
实际上等于
17.689


不幸的是,没有返回整数或浮点的
list_to_number/1
函数。最常见的处理方法是使用
list\u to\u float/1执行try/catch操作,然后返回到
list\u to\u integer/1
。或者,您可以使用或不引发异常(如上所述,使用被认为是一种不好的做法)。

我添加了io的问题!您缺少格式;)我添加了io的问题!您缺少格式;)我无法再测试代码几个小时,但是如果使用
list\u to\u float/1
并返回一个整数,它会导致异常还是会是1.0、2.0等?
list\u to\u float/1
在传递整数的字符串表示形式(例如
“1”
)时肯定会引发badarg错误。这是。我无法再测试代码几个小时,但是如果使用
list\u to\u float/1
并返回一个整数,它是否会导致异常,还是会是1.0、2.0等?
list\u to\u float/1
在传递整数的字符串表示形式(例如
“1”
)时肯定会引发badarg错误。这是。