0

プログラムのメニューとして使用する次のクラスがあります。

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 は、次のようなモジュール クラスに移動する必要があります。

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 クラスに移動していないか、最後までスキップしているようです。ここで私が間違っていることを誰かが指摘できますか?

2012 年 8 月 16 日 14:53 に編集

わかりましたので、提案された変更を行い、次の関数を変更して (v) を含めました。

def navigateTo(what)
what.new(v).display
mainMenu
  end

ただし、エラーのリストを取得しています。

  1. (Application class filepath) in 'mainMenu' uninitialized constant Application: :CourseModules (nameError)

これは、メニューのnavigateTo CourseModules下の行で発生しwhen "1"ます。

  1. 次に、initialize メソッドの行に別のエラーがありmainMenu、'initialize' の from (filepath) とだけ書かれています。

  2. Application.newApplication.rb クラスの末尾の行に from (filepath) 'new' とだけ書かれている同様のものと、同じ行に 'class:Application' と書かれているものがあります。

  3. 次に、「トップ(必須)」の(ファイルパス)という最初の行に1つあります

  4. 最後に、以前のものとはまったく異なる別の 2 つのエラーがあります。

    -e:1:in 'load' から

    -e:1:in 'main' から

私が今間違っていることについて他のアイデアはありますか?

* 2012 年 8 月 16 日 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
4

1 に答える 1

2

アプリケーションが CourseModules メニューを表示しない (つまり、 を呼び出さないaddModule) 理由は、アプリケーションが Application.mainMenu メソッドでスタックしているためです。「1」を押すと、Application.navigateToメソッドが呼び出されます。これにより、表示されている CourseModules オブジェクトが出力#<Module:0x1bd2c90>され、mainMenu が再度表示されます。これを永遠に続けるため、 addModule 行にたどり着くことはありません。

コードにはかなりの問題があるため、修正方法を提案する方法がよくわかりません。例えば、

1) what.new.displayCourseModules の初期化メソッドには 1 つのパラメーターが必要ですが、何も渡していないため失敗します

2) addModulemainMenu メソッドから呼び出したいメソッドは、メソッドが Application クラスに存在しないために失敗します。

3) addModuleCourseModules クラスの navigateTo メソッドが存在しないため、このメソッドは失敗します。

コードの整理方法を再評価する必要があると思います。おそらく、すべてのインターフェース関連のメソッドを 1 つのクラスに入れます。そのクラスは、プロンプトの表示を処理し、入力を適切なクラスに渡して処理します。

于 2012-08-14T17:09:38.607 に答える