问题描述
我正在尝试制作一个程序,该程序将读取一个数字,然后在列表中输出该数字的每个数字。但是,在我尝试使用数字8和9之前,大多数情况看起来都很好。该程序仅输出 \b
\t
代替。
如果输入的数字包含8或9,并且同时还有其他数字,例如 283
,它将正常打印。否则,如果只有8或9,例如 8
, 99
,那么它将为我提供8和9(如果我没记错的话)。
我的程序如下:
digitize(0)-> 0;
digitize(N),当N
当N> = 10->时,
digitize(N)。 digitize(N div 10)++ [N rem 10]。
I am trying to make a program that will read in a number and then output every digit of that number in a list. However, most of the things look fine until I try with number 8 and 9. The program only output \b
\t
instead.
if the input number contains 8 or 9, and in the same time there are other numbers, for example 283
, it will print normally. Otherwise if there is only 8 or 9, such as8
, 99
, then it will give me that binary representation of 8 and 9 (if I remember correctly).
My program is as below:digitize(0)-> 0;
digitize(N) when N < 10 -> [N];
digitize(N) when N >= 10 -> digitize(N div 10)++[N rem 10].
推荐答案
该函数返回预期的列表,但外壳程序将数字列表显示为字符串的ASCII码字符(因为这正是Erlang中的 字符串;没有特殊的字符串类型)。您只需在提示符下输入 [8,8]
(例如),然后通过调用 shell:strings(false)
(和 shell:strings(true)
,当您再次需要正常行为时)。
The function returns the expected list, but the shell shows lists of numbers which are ASCII-codes of characters as strings (because that's just what strings are in Erlang; there's no special string type). You can see it by just entering [8, 8]
(e.g.) at the prompt and disable this behavior by calling shell:strings(false)
(and shell:strings(true)
when you need the normal behavior again).
这篇关于在erlang中打印数字的每个数字时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!