我需要帮助来修复我的代码。因为我最后一次9号失败了。
Q: 填充if_then_elseif((函数,使用for循环和if-then-else语句创建所需的数组。目标是根据以下对应关系创建一个与输入表等效的输出表{"1"、"2"、"3"、"4"、"5"、"6"、"7"、"8"、"9"}=>gt;{"a"、"b"、"c"、"d"、"e"、"f"、"n"、"n-"、"n&"}]]
require "testwell"
function foo(arg)
--arg is the table of numbers
local out = {}
local n = #arg
-- Put your code between here ****************
for i = 1,#arg,1 do
if i==1 then out[i] = "a"
elseif i==2 then out[i] = "b"
elseif i==3 then out[i]= "c"
elseif i==4 then out[i] = "d"
elseif i==5 then out[i]= "e"
elseif i==6 then out[i] = "f"
elseif i==7 then out[i] = "n"
elseif i==8 then out[i]= "n"
elseif i==9 then out[i] = "n"
else print("invalid input")
end
end
-- and here **********************************
return out
end
is(foo({1,2}), {"a", "b"}, 'numbers to letters 1')
is(foo({1,2,3}), {"a", "b", "c"}, 'numbers to letters 2')
is(foo({1,2,3,4}), {"a", "b", "c", "d"}, 'numbers to letters 3')
is(foo({1,2,3,4,5}), {"a", "b", "c", "d", "e"}, 'numbers to letters 4')
is(foo({1,2,3,4,5,6}), {"a", "b", "c", "d", "e", "f"}, 'numbers to letters 5')
is(foo({1,2,3,4,5,6,7}), {"a", "b", "c", "d", "e", "f", "n"}, 'numbers to letters 6')
is(foo({1,2,3,4,5,6,7,8}), {"a", "b", "c", "d", "e", "f", "n", "n"}, 'numbers to letters 7')
is(foo({1,2,3,4,5,6,7,8,9}), {"a", "b", "c", "d", "e", "f", "n", "n", "n"}, 'numbers to letters 8')
is(foo({9,8,7,6,5,4,3,2,1}), {"n", "n", "n", "f", "e", "d", "c", "b", "a"}, 'numbers to letters reversed')
report()
``````````````````````
the output i received is
`````````
Program 'lua.exe' started in 'C:UsersMarianDownloadsZeroBraneStudiomyprograms' (pid: 6032).
ok 1 - numbers to letters 1
ok 2 - numbers to letters 2
ok 3 - numbers to letters 3
ok 4 - numbers to letters 4
ok 5 - numbers to letters 5
ok 6 - numbers to letters 6
ok 7 - numbers to letters 7
ok 8 - numbers to letters 8
not ok 9 - numbers to letters reversed
# Failed test 'numbers to letters reversed'
# in ...ZeroBraneStudiomyprograms290-A2 5-e-if then else.lua at line 54.
# got: {"a", "b", "c", "d", "e", "f", "n", "n", "n"} (table)
# expected: {"n", "n", "n", "f", "e", "d", "c", "b", "a"} (table)
1..9 # Looks like you passed 8 and failed 1 test of 9 (8/0/9).
Program completed in 0.17 seconds (pid: 6032).
`````````````````````
我的个人推荐:
function foo( arg )
--arg is the table of numbers
local map = {'a', 'b', 'c', 'd', 'e', 'f', 'n', 'n', 'n'}
local out = {}
local n = #arg -- not needed.
-- Put your code between here ****************
for key, value in pairs( arg ) do -- note the associative arg is also handled.
out[key] = map[value] or print 'Invalid input' -- no need of if's.
end
-- and here **********************************
return out
end
但是,如果必须使用if
的angarg
是一个纯索引数组:
function foo( arg )
--arg is the table of numbers
local out = {}
local n = #arg
-- Put your code between here ****************
for i = 1, n do
if arg[i] == 1 then out[i] = 'a'
elseif arg[i] == 2 then out[i] = 'b'
elseif arg[i] == 3 then out[i] = 'c'
elseif arg[i] == 4 then out[i] = 'd'
elseif arg[i] == 5 then out[i] = 'e'
elseif arg[i] == 6 then out[i] = 'f'
elseif arg[i] == 7 or arg[i] == 8 or arg[i] == 9 then out[i] = 'n'
else print 'Invalid input' end
end
-- and here **********************************
return out
end
附言我希望你的老师没有读StackOverflow。