设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1854|回复: 4
打印 上一主题 下一主题

[已经解决] 自己做了一个小程序,能不能看看有什么错

[复制链接]

Lv3.寻梦者

梦石
0
星屑
1934
在线时间
403 小时
注册时间
2015-8-30
帖子
395
跳转到指定楼层
1
发表于 2016-1-19 13:25:07 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
本帖最后由 有丘直方 于 2016-1-19 13:26 编辑

最近刚刚开始学RGSS2……想做个脚本程序试试手……不过很遗憾的是第一个脚本就出现一个错误……帮我看看有什么错误好吗……
RUBY 代码复制
  1. #==============================================================================
  2. # ■ Calculator
  3. #------------------------------------------------------------------------------
  4. #  可以计算算式的程序。
  5. #==============================================================================
  6.  
  7. #--------------------------------------------------------------------------
  8. # ● 输入变量
  9. #--------------------------------------------------------------------------
  10. first_count = 34
  11. second_count = 28.67
  12. symbol = "-"
  13. #--------------------------------------------------------------------------
  14. # ● 计算过程
  15. #--------------------------------------------------------------------------
  16. if symbol == "+"        # 加法
  17.   def results
  18.     result = first_count + second_count
  19.     print "#{first_count} + #{second_count} = #{result}"
  20.   end
  21. elsif symbol == "-"     # 减法
  22.   def results
  23.     result = first_count - second_count
  24.     print "#{first_count} - #{second_count} = #{result}"
  25.   end
  26. elsif symbol == "*"     # 乘法
  27.   def results
  28.     result = first_count * second_count
  29.     print "#{first_count} * #{second_count} = #{result}"
  30.   end
  31. elsif symbol == "/"     # 除法
  32.   def results
  33.     result = first_count / second_count
  34.     print "#{first_count} / #{second_count} = #{result}"
  35.   end
  36. elsif symbol == "%"     # 取模
  37.   def results
  38.     result = first_count % second_count
  39.     print "#{first_count} % #{second_count} = #{result}"
  40.   end
  41. elsif symbol == "**"    # 乘方
  42.   def results
  43.     result = first_count ** second_count
  44.     print "#{first_count} ** #{second_count} = #{result}"
  45.   end
  46. else                    # 错误算式
  47.   def results
  48.     print "ERROR!!!"
  49.   end
  50. end
  51. #--------------------------------------------------------------------------
  52. # ● 输出结果
  53. #--------------------------------------------------------------------------
  54. results

提示23行有错误。
line 23: NameError occurred.
undefined local variable or method 'first_count' for main:Object

以上是错误信息,谁能看看有没有什么错?抱歉我看不懂英语。
小仙女一枚~

Lv3.寻梦者

梦石
0
星屑
1934
在线时间
403 小时
注册时间
2015-8-30
帖子
395
2
 楼主| 发表于 2016-1-19 19:17:23 | 显示全部楼层
喵呜喵5 发表于 2016-1-19 15:56
下面是几个可行的解决方法:
1.不使用局部变量
#------------------------------------------------------- ...

谢谢。不过,我有一个问题。最后附上的那个代码26行的“return”是干什么的?我曾以为它只能在函数的定义里表示返回值。
小仙女一枚~
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
1934
在线时间
403 小时
注册时间
2015-8-30
帖子
395
3
 楼主| 发表于 2016-1-19 20:13:56 | 显示全部楼层
喵呜喵5 发表于 2016-1-19 19:28
结束执行(若该方法不需要返回值时,可省略返回值)
在这里的用处是,不执行 case 后面这句代码:
print  ...


谢谢!
小仙女一枚~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-6 12:03

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表