在Lua的io.read并不起作用

时间:2014-10-23 12:58:35

标签: lua

执行此功能时,只显示第二个io.read,第一个被忽略:

function playernames ()
    os.execute ("cls")
    io.write ("\n Player 1, choose a name: ")
    playername1 = io.read ()
    os.execute ("cls")
    io.write ("\n Player 2, choose a name: ")
    playername2 = io.read ()
end

如何正确显示第一个io.read

2 个答案:

答案 0 :(得分:0)

不是我对Lua知之甚少,所以这是一个疯狂的猜测,但不应该是:

function playernames ()
    os.execute ("cls")
    io.write ("Player 1, choose a name: ")
    playername1 = io.read ()
    io.write ("\n")
    os.execute ("cls")
    io.write ("Player 2, choose a name: ")
    playername2 = io.read ()
    io.write ("\n")
end

答案 1 :(得分:0)

好“cls”将清除屏幕,所以如果你想在键入它们后仍然在屏幕上,你需要删除第二个“cls”:

function playernames ()
    os.execute ("cls")
    io.write ("\n Player 1, choose a name: ")
    playername1 = io.read ()
    io.write ("\n Player 2, choose a name: ")
    playername2 = io.read ()
end

playernames ()

如果你只想展示它们,那么:

function playernames ()
    os.execute ("cls")
    io.write ("\n Player 1, choose a name: ")
    playername1 = io.read ()
    os.execute ("cls")
    io.write ("\n Player 2, choose a name: ")
    playername2 = io.read ()
    os.execute ("cls")
    io.write ("Player 1: "..playername1.."\n") --or use print here
    io.write ("Player 2: "..playername2.."\n")
end

playernames ()

否则,如果这不是你要找的答案,你应该指明 “我如何正确显示第一个io.read?”。