<table class="text">
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 函数center,参数words def center(words) do #按长度从小到大来排序 words = Enum.sort_by words,&String.length/1 #求出最大值 max_length = String.length (List.last words) #每一个x,都传给center_word函数 for x <- words,do: IO.puts center_word(x,max_length) end #定义center_word函数,参数为word和length def center_word(word,length) do #算出最大长度减去word的长度,再除以二,filler等于该数值个空格 filler = String.duplicate(" ",div(length - String.length(word),2)) #在word的前后都加上这么多空格 filler <> word <> filler end end 原文链接:https://www.f2er.com/note/421726.html