2017-11-05 4 views

Antwort

1

Datei erstellen calc.rb

class Calc 
    def sum(a,b) 
     a + b 
    end 

    def mul(a,b) 
     a * b 
    end 

    def diff(a,b) 
     a - b 
    end 

    def div(a,b) 
     a/b 
    end 
end 

puts "Enter A" 
a = gets.chomp.to_i 
puts "Enter B" 
b = gets.chomp.to_i 
obj = Calc.new 
p "sum: #{obj.sum(a,b)}" 
p "product: #{obj.mul(a,b)}" 
p "quotient: #{obj.div(a,b)}" 
p "difference: #{obj.diff(a,b)}" 

Lauf dieses in Konsole

Rubin calc.rb

Enter A 
4 
Enter B 
2 
"sum: 6" 
"product: 8" 
"quotient: 2" 
"difference: 2" 
+0

Dank Nataraja. Nachdem ich das Problem einige Zeit studiert hatte, schaffte ich es, eine etwas andere Lösung zu schreiben. Ich schätze deine Hilfe. Ich bin ein Neuling in einem Coding Bootcamp! ! ! –