我有以下课程,我将用作我的课程菜单:
class Application
# To change this template use File | Settings | File Templates.
def initialize
mainMenu
end
def navigateTo(what)
what.new.display
mainMenu
end
def mainMenu
puts "What would you like to do?
1: Add module to a scheme
2: Remove module from a scheme
3: Query modules
4: Modify module
5: Register a student on a scheme
6: Remove a student from a scheme
7: Register a student on a module
8: Remove a student from a module"
case gets.strip
when "1"
navigateTo Module
addModule
when "2"
navigateTo Module
when "3"
navigateTo Module
when "4"
navigateTo Module
when "5"
navigateTo Student
when "6"
navigateTo Student
when "7"
navigateTo Student
end
end
Application.new
end
然而,当我运行该课程时,我尝试从菜单中选择选项1,并且该行已在控制台中打印出来:
#<Module:0x1bd2c90>What would you like to do?
然后再次打印出菜单。
选项1应导航到我的Module类,如下所示:
class Module
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
def self.moduleYear
@@moduleYear
end
def initialize(v)
@val = v
end
# Set and get the @val object value
def set (v)
@val = v
end
def get
return @val
end
def addModule
moduleName = Module.new(30)
moduleRefNo = Random(100)
#moduleTitle = @moduleTitle
moduleYear(4)
print "What is the name of the module you would like to add?"
moduleName = gets
moduleRefNo
printf "Which year does the module belong to?"
@@moduleYear = gets
puts "#{moduleName}, belonging to #{@@moduleYear} has been added to the system, with reference number #{moduleRefNo}."
navigateTo Application
end
def addModuleToScheme
moduleName.moduleScheme = schemeName
end
def removeModuleFromScheme
moduleName.moduleScheme = nil
end
def queryModule
end
end
一旦用户从主菜单中选择了选项1,并且程序已导航到Module类,我希望它完全运行该类,即向用户显示提示,并读取他们输入的任何内容键盘,然后导航回菜单,如行
所示navigateTo Application
在我的'addModule'函数的末尾。但是,出于某种原因,它似乎要么不是导航到Module类,要么直接跳到它的末尾。谁能指出我在这里做错了什么?
编辑16/08/2012 14:53
好的,所以我已经进行了修改并更改了以下功能以包含(v):
def navigateTo(what)
what.new(v).display
mainMenu
end
但是,我现在得到一个错误列表:
这发生在菜单中navigateTo CourseModules
下的when "1"
行。
然后在初始化方法中的行mainMenu
上出现了另一个错误,它只是从'initialize'中的(filepath)说的
在Application.rb类末尾的行Application.new
上用(filepath)'new'表示的类似的一个,以及在'class:Application'中的一个同一行
然后我在第一行中有一个说'top(required)'中的(filepath)
最后,我还有另外两个错误,与之前的错误完全不同:
from -e:1:in'load'
来自-e:1:在'main'
中
关于我现在做错的任何其他想法?
* 编辑16/08/2012 16:15 *
我的Application.rb类的完整脚本是:
class Application
# To change this template use File | Settings | File Templates.
def initialize
mainMenu
end
def navigateTo(what)
what.new(v).display
mainMenu
end
def mainMenu
puts "What would you like to do?
1: Add module to a scheme
2: Remove module from a scheme
3: Query modules
4: Modify module
5: Register a student on a scheme
6: Remove a student from a scheme
7: Register a student on a module
8: Remove a student from a module"
case gets.strip
when "1"
navigateTo CourseModules
when "2"
navigateTo CourseModules
when "3"
navigateTo CourseModules
when "4"
navigateTo CourseModules
when "5"
navigateTo Student
when "6"
navigateTo Student
when "7"
navigateTo Student
end
end
Application.new
end
我的courseModules.rb类的完整脚本是:
class CourseModules
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
def self.moduleYear
@@moduleYear
end
def initialize(v)
@val = v
end
# Set and get the @val object value
def set (v)
@val = v
end
def get
return @val
end
def addModule
moduleName = Module.new(30)
moduleRefNo = Random(100)
#moduleTitle = @moduleTitle
moduleYear(4)
print "What is the name of the module you would like to add?"
moduleName = gets
moduleRefNo
printf "Which year does the module belong to?"
@@moduleYear = gets
puts "#{moduleName}, belonging to #{@@moduleYear} has been added to the system, with reference number #{moduleRefNo}."
navigateTo Application
end
def addModuleToScheme
moduleName.moduleScheme = schemeName
end
def removeModuleFromScheme
moduleName.moduleScheme = nil
end
def queryModule
end
end
答案 0 :(得分:2)
您的应用程序从未显示您的CourseModules菜单(即从不调用addModule
)的原因是因为它停留在Application.mainMenu方法中。按“1”时,将调用Application.navigateTo
方法。这将输出CourseModules对象,即您看到的#<Module:0x1bd2c90>
,然后再次显示mainMenu。你一直这样做,这就是为什么你永远不会到达addModule行。
代码存在很多问题,所以我不确定如何建议修复。例如,
1)what.new.display
将失败,因为CourseModules初始化方法需要1个参数,但是你没有传递
2)由于Application类中不存在方法,您要从mainMenu方法调用的addModule
方法将失败。
3)addModule
方法将失败,因为CourseModules类不存在navigateTo方法。
我认为你需要重新评估你是如何组织代码的。也许把所有与接口相关的方法放在一个类中。该类将处理显示提示并将任何输入传递给适当的类进行处理。