本帖最后由 喵呜喵5 于 2016-1-19 16:10 编辑
下面是几个可行的解决方法:
1.不使用局部变量
#-------------------------------------------------------------------------- # ● 输入变量 #-------------------------------------------------------------------------- First_count = 34 Second_count = 28.67 Symbol = "+" #-------------------------------------------------------------------------- # ● 计算过程 #-------------------------------------------------------------------------- if Symbol == "+" # 加法 def results result = First_count + Second_count print "#{First_count} + #{Second_count} = #{result}" end end #-------------------------------------------------------------------------- # ● 输入变量 #-------------------------------------------------------------------------- @first_count = 34 @second_count = 28.67 @symbol = "+" if @symbol == "+" # 加法 def results result = @first_count + @second_count print "#{@first_count} + #{@second_count} = #{result}" end end #-------------------------------------------------------------------------- # ● 输出结果 #-------------------------------------------------------------------------- results
#--------------------------------------------------------------------------
# ● 输入变量
#--------------------------------------------------------------------------
First_count = 34
Second_count = 28.67
Symbol = "+"
#--------------------------------------------------------------------------
# ● 计算过程
#--------------------------------------------------------------------------
if Symbol == "+" # 加法
def results
result = First_count + Second_count
print "#{First_count} + #{Second_count} = #{result}"
end
end
#--------------------------------------------------------------------------
# ● 输入变量
#--------------------------------------------------------------------------
@first_count = 34
@second_count = 28.67
@symbol = "+"
if @symbol == "+" # 加法
def results
result = @first_count + @second_count
print "#{@first_count} + #{@second_count} = #{result}"
end
end
#--------------------------------------------------------------------------
# ● 输出结果
#--------------------------------------------------------------------------
results
2.别用这种古怪的思路,而是用正常的思路写这段代码(推荐)
#-------------------------------------------------------------------------- # ● 输入变量 #-------------------------------------------------------------------------- first_count = 34 second_count = 28.67 symbol = "+" def result(first_count, second_count, symbol) if symbol == "+" result = first_count + second_count print "#{first_count} + #{second_count} = #{result}" end end #-------------------------------------------------------------------------- # ● 输出结果 #-------------------------------------------------------------------------- result(first_count, second_count, symbol)
#--------------------------------------------------------------------------
# ● 输入变量
#--------------------------------------------------------------------------
first_count = 34
second_count = 28.67
symbol = "+"
def result(first_count, second_count, symbol)
if symbol == "+"
result = first_count + second_count
print "#{first_count} + #{second_count} = #{result}"
end
end
#--------------------------------------------------------------------------
# ● 输出结果
#--------------------------------------------------------------------------
result(first_count, second_count, symbol)
附上修改后的代码:
#============================================================================== # ■ Calculator #------------------------------------------------------------------------------ # 可以计算算式的程序。 #============================================================================== #-------------------------------------------------------------------------- # ● 输入变量 #-------------------------------------------------------------------------- first_count = 34 second_count = 28.67 symbol = "-" #-------------------------------------------------------------------------- # ● 计算过程 #-------------------------------------------------------------------------- def results(first_count, second_count, symbol) case symbol when '+' then result = first_count + second_count when '-' then result = first_count - second_count when '*' then result = first_count * second_count when '/' then result = first_count / second_count when '%' then result = first_count % second_count when '**' then result = first_count ** second_count else print "ERROR!!!" return end print "#{first_count} #{symbol} #{second_count} = #{result}" end #-------------------------------------------------------------------------- # ● 输出结果 #-------------------------------------------------------------------------- results(first_count, second_count, symbol)
#==============================================================================
# ■ Calculator
#------------------------------------------------------------------------------
# 可以计算算式的程序。
#==============================================================================
#--------------------------------------------------------------------------
# ● 输入变量
#--------------------------------------------------------------------------
first_count = 34
second_count = 28.67
symbol = "-"
#--------------------------------------------------------------------------
# ● 计算过程
#--------------------------------------------------------------------------
def results(first_count, second_count, symbol)
case symbol
when '+' then result = first_count + second_count
when '-' then result = first_count - second_count
when '*' then result = first_count * second_count
when '/' then result = first_count / second_count
when '%' then result = first_count % second_count
when '**' then result = first_count ** second_count
else
print "ERROR!!!"
return
end
print "#{first_count} #{symbol} #{second_count} = #{result}"
end
#--------------------------------------------------------------------------
# ● 输出结果
#--------------------------------------------------------------------------
results(first_count, second_count, symbol)
|