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

Project1

 找回密码
 注册会员
搜索
查看: 1839|回复: 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

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

Lv5.捕梦者 (暗夜天使)

只有笨蛋才会看到

梦石
1
星屑
20935
在线时间
9332 小时
注册时间
2012-6-19
帖子
7106

开拓者短篇九导演组冠军

2
发表于 2016-1-19 15:56:25 | 只看该作者
本帖最后由 喵呜喵5 于 2016-1-19 16:10 编辑

下面是几个可行的解决方法:
1.不使用局部变量
RUBY 代码复制
  1. #--------------------------------------------------------------------------
  2. # ● 输入变量
  3. #--------------------------------------------------------------------------
  4. First_count = 34
  5. Second_count = 28.67
  6. Symbol = "+"
  7. #--------------------------------------------------------------------------
  8. # ● 计算过程
  9. #--------------------------------------------------------------------------
  10. if Symbol == "+"        # 加法
  11.   def results
  12.     result = First_count + Second_count
  13.     print "#{First_count} + #{Second_count} = #{result}"
  14.   end
  15. end
  16.  
  17. #--------------------------------------------------------------------------
  18. # ● 输入变量
  19. #--------------------------------------------------------------------------
  20. @first_count = 34
  21. @second_count = 28.67
  22. @symbol = "+"
  23. if @symbol == "+"        # 加法
  24.   def results   
  25.     result = @first_count + @second_count
  26.     print "#{@first_count} + #{@second_count} = #{result}"
  27.   end
  28. end
  29. #--------------------------------------------------------------------------
  30. # ● 输出结果
  31. #--------------------------------------------------------------------------
  32. results


2.别用这种古怪的思路,而是用正常的思路写这段代码(推荐)
RUBY 代码复制
  1. #--------------------------------------------------------------------------
  2. # ● 输入变量
  3. #--------------------------------------------------------------------------
  4. first_count = 34
  5. second_count = 28.67
  6. symbol = "+"
  7.  
  8. def result(first_count, second_count, symbol)
  9.   if symbol == "+"  
  10.     result = first_count + second_count
  11.     print "#{first_count} + #{second_count} = #{result}"
  12.   end
  13. end
  14. #--------------------------------------------------------------------------
  15. # ● 输出结果
  16. #--------------------------------------------------------------------------
  17. result(first_count, second_count, symbol)



附上修改后的代码:
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. def results(first_count, second_count, symbol)
  17.   case symbol
  18.   when '+'  then result = first_count + second_count
  19.   when '-'  then result = first_count - second_count
  20.   when '*'  then result = first_count * second_count
  21.   when '/'  then result = first_count / second_count
  22.   when '%'  then result = first_count % second_count
  23.   when '**' then result = first_count ** second_count
  24.   else
  25.     print "ERROR!!!"
  26.     return
  27.   end
  28.   print "#{first_count} #{symbol} #{second_count} = #{result}"
  29. end
  30. #--------------------------------------------------------------------------
  31. # ● 输出结果
  32. #--------------------------------------------------------------------------
  33. results(first_count, second_count, symbol)

评分

参与人数 2星屑 +200 梦石 +1 收起 理由
丿梁丶小柒 + 1 认可答案
VIPArcher + 200 我很赞同

查看全部评分

回复 支持 反对

使用道具 举报

Lv3.寻梦者

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

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

使用道具 举报

Lv5.捕梦者 (暗夜天使)

只有笨蛋才会看到

梦石
1
星屑
20935
在线时间
9332 小时
注册时间
2012-6-19
帖子
7106

开拓者短篇九导演组冠军

4
发表于 2016-1-19 19:28:31 | 只看该作者
有丘直方 发表于 2016-1-19 19:17
谢谢。不过,我有一个问题。最后附上的那个代码26行的“return”是干什么的?我曾以为它只能在函数的定义 ...

结束执行(若该方法不需要返回值时,可省略返回值)
在这里的用处是,不执行 case 后面这句代码:
print "#{first_count} #{symbol} #{second_count} = #{result}"
回复 支持 反对

使用道具 举报

Lv3.寻梦者

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


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

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-4-24 06:50

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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