2011-03-10 63 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