lua-users home
lua-l archive

Re: Make numbers limited to 0-359 as input or output

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 07/20/2018 07:50 AM, Bill Kelsoe wrote:
> Hi, I recently built a sample code for calculating reciprocal winds for
> flight simulation purposes. I want to limit user input to numbers from
> 0-359 inclusive. Nothing like 360 should be entered by user. How can I do
> this?
> 
> local line
> repeat
> print("Input wind heading: ");
> line = io.read() -- read a line
> n = tonumber(line) -- try to convert it to a number
> k = tonumber(line)
> k = 180-n
> if n > 180 then
> print("Reciprocal heading is: ",-k)
> elseif n < 180 then
> print("Reciprocal heading is: ", n+180)
> else n = 180
> print ("are you kidding me?")
> end
> print("do you want to convert again? y/n?")
> line = io.read()
> until line ~= "y"
> print("Goodbye")
> 
> Also how can I input this into the source code executable to run it? If I
> press enter after the first line it just stops at local line.
> 
> Thanks in advance
> Adel
local answer = 'y'
while (answer == 'y') do
 io.write('Input wind heading: ')
 local s = io.read()
 local wind_direction
 local n = tonumber(s)
 if
 not (n and (n >= 0) and (n <= 359) and (math.type(n) == 'integer'))
 then
 print('are you kidding me?')
 else
 wind_direction = n
 end
 if wind_direction then
 -- Rotate by 180 deg:
 wind_direction = (wind_direction + 180) % 360
 print(('Reciprocal heading is: %d'):format(wind_direction))
 end
 io.write('do you want to convert again? y/n?: ')
 answer = io.read()
end
-- Martin

AltStyle によって変換されたページ (->オリジナル) /