0
當我試圖做一個循環我的代碼,它突然問我這個:未定義的局部變量或方法'停止」主:對象(NameError)
未定義的局部變量或方法`停'for main:Object(NameError)。
如何解決此錯誤?
loop do
puts "Hello. I am a calculator. Please tell me your name."
name = gets
puts "Hi #{name}. Enter 1 to add, 2 to multiply, 3 to subtract, and 4 to divide."
z = gets.chomp.to_i
def addition
puts "Please enter your first number"
a = gets.chomp.to_i
puts "Please enter your second number"
b = gets.chomp.to_i
puts a+b
end
def multiply
puts "Please enter your first number"
c = gets.chomp.to_i
puts "Please enter your second number"
d = gets.chomp.to_i
puts c*d
end
def subtract
puts "Please enter your first number"
e = gets.chomp.to_i
puts "Please enter your second number"
f = gets.chomp.to_i
puts e-f
end
def divide
puts "Please enter your first number"
h = gets.chomp.to_i
puts "Please enter your second number"
g = gets.chomp.to_i
puts h/g
end
if z == 1
puts addition
elsif z == 2
puts multiply
elsif z == 3
puts subtract
elsif z == 4
puts divide
end
puts "Would you like to calculate something else or stop?"
choice = gets.chomp.to_s
if choice == stop
break
end
end
歡迎堆棧溢出。請閱讀你的問題,並想象你已經被同事問過同樣的問題。你想看到什麼來幫助回答這個問題?將此添加到問題中。正如你的問題是非常模糊,需要詳細信息,如果我們要給你任何詳細的幫助。 –
對不起,代碼變得有點怪怪 –
停止應該是「停止」。如果你沒有用引號括起來,系統會認爲它是一個變量,這就是爲什麼你會得到這個錯誤。 – artsylar