2011-03-10 66 views
5

我想在Ruby中創建一個菜單,以便取決於用戶輸入的內容,取決於調用的是什麼類。然後,在這種情況下,它將返回到「Main」或類「Options」。Ruby中的選項菜單

我希望有人能幫助我。這是我的代碼。

module Physics 
G = 21 
C = 20000 
Pi = 3.14 
D = 100 
end 

class Options 
puts "Please select 1 for Acceleration and 2 for Energy." 
option = gets() 
if option == 1 
then 
puts "AccelCalc" # This is the bit that needs to direct the user to the class AccelCalc 
else 
puts "EnergyCalc" # This needs to take them to EnergyCalc. 
end 
end 

class AccelCalc 
include Physics 
puts "Please enter the mass of the object" 
M = gets() 
puts "The mass of the object is " + M + "." 
puts "Please enter the speed of the defined object." 
S = gets() 
puts "The speed of the object is set at " + S + "." 
puts "The acceleration will now be calculated." 
puts S.to_i*M.to_i 
end 

class EnergyCalc 
include Physics 
puts "This will use the function E=MC^2 to calculate the Energy." 
puts "Enter object mass" 
M = gets() 
puts "The mass is " + M + "." 
puts "Now calculating the Energy of the object." 
puts M.to_i*C_to.i**2 
end 
$end 

我也希望能夠在課後被調用返回到clas選項。我確信這很容易,但我無法解決。

再次謝謝你,

羅斯。

+0

那麼,什麼是問題?我只能看到該文中的陳述。 – DarkDust 2011-03-10 09:55:41

回答

0

雖然我真的不明白這到底是怎麼想,一兩件事,馬上跳出來我的眼睛是塊:

option = gets() 
if option == 1 
then 
puts "AccelCalc" # This is the bit that needs to direct the user to the class AccelCalc 
else 
puts "EnergyCalc" # This needs to take them to EnergyCalc. 
end 

gets返回一個字符串。所以,你應該做的:

case gets().strip() 
when "1" 
    puts "AccelCalc" 
when "2" 
    puts "EnergyCalc" 
else 
    puts "Invalid input." 
end 

我在這裏明確的使用括號,而不是gets().strip()可以簡單的寫gets.strip在Ruby中。這個表達式所做的是從標準輸入中讀取一些東西,並刪除它周圍的所有空白(按回車鍵換行)。然後比較結果字符串。

+0

好吧,現在我得到「輸入無效」。選擇一個項目後,無論是1還是2,我都會輸入一個總是輸出該項目的項目。輸入任意數字後,進程運行AccelCalc類,然後,不返回Options類,而是運行EnergyCalc,然後終止。 – RossDoughty 2011-03-10 10:13:04

+0

對不起,但我真的沒有得到你在做什麼或試圖做什麼......真正讓我困惑的是爲什麼你把大部分代碼放到*類體*而不是方法中......這樣的代碼是隻有在定義類時才執行。 – DarkDust 2011-03-10 10:24:58

1

我正要給你只是一些使用技巧 - 但你的代碼做了不少事情不對:

  1. 壓痕 - 使你的代碼可讀性!
  2. 類中的代碼在ruby中有效,但這並不是一個好主意 - 它主要用於元編程。
  3. 不要將用戶輸入捕獲到常量中。他們不是。

那麼我怎麼編碼呢?類似這樣的:

class App 

    def initialize 
    main_menu 
    end 

    def navigate_to(what) 
    what.new.display 
    main_menu 
    end 

    def main_menu 
    puts "Please select 1 for Acceleration and 2 for Energy." 
    case gets.strip 
    when "1" 
     navigate_to AccelCalc 
    when "2" 
     navigate_to EnergyCalc 
    else 
     puts "Choose either 1 or 2." 
     main_menu 
    end 
    end 
end 

class AccelCalc 
    include Physics 
    def display 
    puts "Please enter the mass of the object" 
    @m = gets.to_f 
    puts "The mass of the object is #{@m}." 
    puts "Please enter the speed of the defined object." 
    @s = gets.to_f 
    puts "The speed of the object is set at #{@s}." 
    puts "The acceleration will now be calculated." 
    puts @s * @m 
    end 
end 

# ... 

App.new # Run teh codez 
10

您可以檢出Highline。這是創建控制檯應用程序的一個很好且簡單的框架。與

sudo的創業板安裝它安裝--no-RDoc的--no裏高架

下面是一個例子。

require "rubygems" 
require "highline/import" 

@C = 299792458 

def accel_calc 
    mass = ask("Mass? ", Float) 
    speed = ask("Speed? ", Float) 
    puts 
    puts("mass * speed = #{mass*speed}") 
    puts 
end 

def energy_calc 
    mass = ask("Mass? ", Float) 
    puts 
    puts("E=MC^2 gives #{mass*@C**2}") 
    puts 
end 

begin 
    puts 
    loop do 
    choose do |menu| 
     menu.prompt = "Please select calculation " 
     menu.choice(:Acceleration) { accel_calc() } 
     menu.choice(:Energy) { energy_calc() } 
     menu.choice(:Quit, "Exit program.") { exit } 
    end 
    end 
end 
+0

絕對是解決這個問題的最好方法。 – 2011-03-10 17:14:47