Project1

标题: 自己做了一个小程序,能不能看看有什么错 [打印本页]

作者: 有丘直方    时间: 2016-1-19 13:25
标题: 自己做了一个小程序,能不能看看有什么错
本帖最后由 有丘直方 于 2016-1-19 13:26 编辑

最近刚刚开始学RGSS2……想做个脚本程序试试手……不过很遗憾的是第一个脚本就出现一个错误……帮我看看有什么错误好吗……
[pre lang="ruby"]#==============================================================================
# ■ Calculator
#------------------------------------------------------------------------------
#  可以计算算式的程序。
#==============================================================================

#--------------------------------------------------------------------------
# ● 输入变量
#--------------------------------------------------------------------------
first_count = 34
second_count = 28.67
symbol = "-"
#--------------------------------------------------------------------------
# ● 计算过程
#--------------------------------------------------------------------------
if symbol == "+"        # 加法
  def results
    result = first_count + second_count
    print "#{first_count} + #{second_count} = #{result}"
  end
elsif symbol == "-"     # 减法
  def results
    result = first_count - second_count
    print "#{first_count} - #{second_count} = #{result}"
  end
elsif symbol == "*"     # 乘法
  def results
    result = first_count * second_count
    print "#{first_count} * #{second_count} = #{result}"
  end
elsif symbol == "/"     # 除法
  def results
    result = first_count / second_count
    print "#{first_count} / #{second_count} = #{result}"
  end
elsif symbol == "%"     # 取模
  def results
    result = first_count % second_count
    print "#{first_count} % #{second_count} = #{result}"
  end
elsif symbol == "**"    # 乘方
  def results
    result = first_count ** second_count
    print "#{first_count} ** #{second_count} = #{result}"
  end
else                    # 错误算式
  def results
    print "ERROR!!!"
  end
end
#--------------------------------------------------------------------------
# ● 输出结果
#--------------------------------------------------------------------------
results[/pre]
提示23行有错误。
line 23: NameError occurred.
undefined local variable or method 'first_count' for main:Object

以上是错误信息,谁能看看有没有什么错?抱歉我看不懂英语。
作者: 喵呜喵5    时间: 2016-1-19 15:56
本帖最后由 喵呜喵5 于 2016-1-19 16:10 编辑

下面是几个可行的解决方法:
1.不使用局部变量
[pre lang="ruby"]#--------------------------------------------------------------------------
# ● 输入变量
#--------------------------------------------------------------------------
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[/pre]

2.别用这种古怪的思路,而是用正常的思路写这段代码(推荐)
[pre lang="ruby"]#--------------------------------------------------------------------------
# ● 输入变量
#--------------------------------------------------------------------------
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)[/pre]


附上修改后的代码:
[pre lang="ruby"]#==============================================================================
# ■ 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)[/pre]

作者: 有丘直方    时间: 2016-1-19 19:17
喵呜喵5 发表于 2016-1-19 15:56
下面是几个可行的解决方法:
1.不使用局部变量
#------------------------------------------------------- ...

谢谢。不过,我有一个问题。最后附上的那个代码26行的“return”是干什么的?我曾以为它只能在函数的定义里表示返回值。
作者: 喵呜喵5    时间: 2016-1-19 19:28
有丘直方 发表于 2016-1-19 19:17
谢谢。不过,我有一个问题。最后附上的那个代码26行的“return”是干什么的?我曾以为它只能在函数的定义 ...

结束执行(若该方法不需要返回值时,可省略返回值)
在这里的用处是,不执行 case 后面这句代码:
print "#{first_count} #{symbol} #{second_count} = #{result}"
作者: 有丘直方    时间: 2016-1-19 20:13
喵呜喵5 发表于 2016-1-19 19:28
结束执行(若该方法不需要返回值时,可省略返回值)
在这里的用处是,不执行 case 后面这句代码:
print  ...


谢谢!




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X5.0