如何读取Erlang中的整数? [英] How to read integer in Erlang?

查看:148
本文介绍了如何读取Erlang中的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取整数的用户输入。 (like cin >> nInput; in C ++)

我发现io:fread bif from http://www.erlang.org/doc/man/io.html ,所以我写这样的代码。

I'm trying to read user input of integer. (like cin >> nInput; in C++)
I found io:fread bif from http://www.erlang.org/doc/man/io.html, so I write code like this.


{ok,X} = io:fread(input:,
〜d),
io:format(〜p〜n,[X])

{ok, X} = io:fread("input : ", "~d"),
io:format("~p~n", [X]).

但是当我输入10时,erlang终端不断给我\而不是10.我假设fread自动读取10并将其转换为字符串。如何直接读取整数值?有没有办法做到这一点?感谢您阅读本文。

but when I input 10, the erlang terminal keep giving me "\n" not 10. I assume fread automatically read 10 and conert this into string. How can I read integer value directly? Is there any way to do this? Thank you for reading this.

推荐答案

1> {ok, [X]} = io:fread("input : ", "~d").
input : 10
{ok,"\n"}
2> X.
10
3> {ok, [A,B]} = io:fread("input : ", "~d,~d").
input : 456,26
{ok,[456,26]}

这就是。

这篇关于如何读取Erlang中的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆