Lua表中的多个输入

时间:2016-03-17 17:49:50

标签: lua lua-table

我是lua的新手,我有这个任务,其中一个我真正难以理解的问题是:

“当用户输入999分数时,将重复询问用户学生分数的程序将停止,然后程序应计算并显示输入的分数,最高分数,最低分数和平均分数。如果用户输入的分数小于零或大于100,请务必显示错误消息。“

我整个星期一直都在这里,仍然无法弄清楚该做什么以及应该在晚上11:59到期。任何见解和方向都会很棒。

- 如何在不断增长的表格中输入多个值= {}?在用户输入999并结束重复循环后,大小由变量s的输入数给出。这实际上是我最大的问题。

我的代码:

 local scores = {}, avg


   repeat
    io.write("Enter score(s)")
local s = tonumber(io.read())  --input and convert data type
    print(s, type(s))     --s value, check input type


if(s < 0 or s > 100) then
print("Error.")  
end



    until (s == 999)

for i = 0, #s, 1 do
sum = 0
if s then 
    sum = sum + s
end

end


-- -----------------------------------------------------------Attemps to find a way to put s values in scores table-----------------------------------------------------------------------------------------
--[[scores[#scores+1] = s  ----Attempt 1  
print (scores)

for i = 0, #s, 1 do    ----Attempt 2
    scores{s} = s[i] 
print (i, scores)                                                   --tried a multitude of different ways and
                                                                       --kept getting the same number printed once or memory location of last entered number
end

for i, s in ipairs (scores) do   --Attempt 3
print (i, s)
end

for i = 0, #s, 1 do
sum = 0
if s then 
    sum = sum + s
end

end --]]
 -- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------

--[[function average(myTable)
local sum = 0

for i in scores do

sum = sum + i
end
return (sum / #scores)
end




print ("The number of values in the table"..#scores)
print ("The average of the scores is "..average(s))
print ("The max value in the table is "..math.max(s))           
print ("The minimum value in the table is "..math.min(s))


  table.maxn(scores), table.minn(scores)
--]]

io.write("Please press enter to continue")
io.read()

0 个答案:

没有答案