如何从输入表中创建输出表- Lua?
2020-9-27 14:36:2
收藏:0
阅读:105
评论:1
我需要帮忙修复我的代码。由于我未完成第9个问题。
Q:请填写if_then_elseif()函数,使用for循环和if then else语句创建所需的数组。目标是创建与输入表等效的输出表,如下对应关系所示 {"1", "2", "3", "4", "5", "6", "7", "8", "9"} =>> {"a", "b", "c", "d", "e", "f", "n", "n", "n"} ]]
require "testwell"
function foo(arg)
--arg是数字表
local out = {}
local n = #arg
--在这里放置代码 ****************
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("无效输入")
end
end
-- 和 **********************************
return out
end
is(foo({1,2}), {"a", "b"}, '数字转字母 1')
is(foo({1,2,3}), {"a", "b", "c"}, '数字到字母 2')
is(foo({1,2,3,4}), {"a", "b", "c", "d"}, '数字转字母 3')
is(foo({1,2,3,4,5}), {"a", "b", "c", "d", "e"}, '数字到字母 4')
is(foo({1,2,3,4,5,6}), {"a", "b", "c", "d", "e", "f"}, '数字到字母 5')
is(foo({1,2,3,4,5,6,7}), {"a", "b", "c", "d", "e", "f", "n"}, '数字到字母 6')
is(foo({1,2,3,4,5,6,7,8}), {"a", "b", "c", "d", "e", "f", "n", "n"}, '数字到字母 7')
is(foo({1,2,3,4,5,6,7,8,9}), {"a", "b", "c", "d", "e", "f", "n", "n", "n"}, '数字到字母 8')
is(foo({9,8,7,6,5,4,3,2,1}), {"n", "n", "n", "f", "e", "d", "c", "b", "a"}, '数字转字母(反转)')
报告()
``````````````````````
我收到的输出是
`````````
程序'lua.exe'在'C:\Users\Marian\Downloads\ZeroBraneStudio\myprograms'(pid:6032)中启动。
ok 1 - 数字转字母 1
ok 2 - 数字到字母 2
ok 3 - 数字转字母 3
ok 4 - 数字到字母 4
ok 5 - 数字到字母 5
ok 6 - 数字到字母 6
ok 7 - 数字转字母 7
ok 8 - 数字转字母 8
未通过的测试9号-数字转字母(反转)
# 在... \ ZeroBraneStudio \ myprograms \ 290-A2 \ 05-e-if then else.lua 第54行的 '数字转字母(反转)'!
# 得到:{"a", "b", "c", "d", "e", "f", "n", "n", "n"} (table)
# 预期的是:{"n", "n", "n", "f", "e", "d", "c", "b", "a"} (table)
1..9#看起来您通过了8个测试,失败了1个测试(8/0/9)。
程序完成,用时0.17秒(pid:6032)。
`````````````````````
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

我的个人建议:
function foo( arg ) --arg 是数字的表 local map = {'a', 'b', 'c', 'd', 'e', 'f', 'n', 'n', 'n'} local out = {} local n = #arg -- 不需要。 - 在这里放置您的代码 **************** - for key,value in对(arg) do中的对中,注意关联即arg也用于处理。 out [key] = map [value] or打印“无效输入” -没有需要的if。 end - 并在这里 ********************************- 返回输出 end但是如果您必须使用“if”和“arg”是一个纯索引数组:
function foo( arg ) --arg 是数字的表 local out = {} local n = #arg - 在这里放置您的代码 **************** - for i = 1,n的do 如果arg [i] == 1,则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或arg [i] == 8或arg [i] == 9 then out [i] ='n' else打印'无效输入'end end - 并在这里 ********************************- 返回输出 endP.S.我希望您的老师不要阅读StackOverflow。