Lua 检查用户是否';s输入的数字是正的或负的,如果是负的,则给出错误

Lua 检查用户是否';s输入的数字是正的或负的,如果是负的,则给出错误,lua,Lua,我正在尝试创建一个程序,它接受用户输入的宽度和高度,并输出矩形的面积和周长。我还希望程序检查用户是否为宽度或高度输入正值或负值。如果用户输入负值,我希望它显示一个错误。这是我到目前为止所做的,但它只是略过了,仍然在计算 local width, height, area, perimeter -- intro text and user inputs print("Welcome to the Rectangle Area & Perimeter Calculator") print

我正在尝试创建一个程序,它接受用户输入的宽度和高度,并输出矩形的面积和周长。我还希望程序检查用户是否为宽度或高度输入正值或负值。如果用户输入负值,我希望它显示一个错误。这是我到目前为止所做的,但它只是略过了,仍然在计算

local width, height, area, perimeter

-- intro text and user inputs
print("Welcome to the Rectangle Area & Perimeter Calculator")

print("Enter Rectangle Width")
width = io.read("n")
if width <0 
then print "Error: Please enter a positive value for width"
end

print("Enter Rectangle Height")
height = io.read("n")
if height <0
then print "Error: Please enter a positive value for height"
end 

--Calculator
area = width * height
print("The area of the rectangle is ", area)
perimeter = 2 * (width + height)
print("The perimeter of the rectangle is ", perimeter)
局部宽度、高度、面积、周长
--介绍文本和用户输入
打印(“欢迎使用矩形面积和周长计算器”)
打印(“输入矩形宽度”)
宽度=io读取(“n”)

如果宽度编辑2:您可以使用
重复
直到
循环进行此操作,因此代码块将重复,直到用户输入的数字为正,请尝试下面的代码

local width, height, area, perimeter

-- intro text and user inputs
print("Welcome to the Rectangle Area & Perimeter Calculator")

repeat
print("Enter Rectangle Width")
width = io.read("n")
  if(width < 0)
    then print("error : enter a positive value")
  end
until width > 0 

repeat
print("Enter Rectangle Height")
height = io.read("n")
  if height <0
    then print "Error: Please enter a positive value for height"
  end  
until height > 0 

--Calculator
area = width * height
print("The area of the rectangle is ", area)
perimeter = 2 * (width + height)
print("The perimeter of the rectangle is ", perimeter)
局部宽度、高度、面积、周长
--介绍文本和用户输入
打印(“欢迎使用矩形面积和周长计算器”)
重复
打印(“输入矩形宽度”)
宽度=io读取(“n”)
如果(宽度<0)
然后打印(“错误:输入正值”)
结束
直到宽度>0
重复
打印(“输入矩形高度”)
高度=输入输出读数(“n”)
如果高度为0
--计算器
面积=宽度*高度
打印(“矩形区域为”,区域)
周长=2*(宽度+高度)
打印(“矩形的周长为”,周长)

如果宽度Lua 5.3不需要它选项名称不再有起始“*”。为了兼容性,Lua将继续接受(并忽略)此字符。
使用return不重新打印(“输入矩形宽度”)Width=io。read(“n”)如果用户继续输入负数,我希望它继续重新要求用户输入宽度。如果输入正数,让程序前进到下一个代码:打印(“输入矩形高度”)高度=io.read(“n”)