小编Rog*_*Wav的帖子

在Ruby中编写基本计算器

这是我第一次涉足计算机编程.我选择学习Ruby,我很享受它.但是,我有点困惑的是为什么答案在这段代码中输出不正确.

def addition_function
    puts "Which numbers would you like to add?"
    @n1 = gets.chomp
    @n2 = gets.chomp
    @n1 + @n2 == @answer
    puts "The sum is... #{@answer}"
end

def subtraction_function
    puts "Which numbers would you like to subtract?"
    @n1 = gets.chomp.to_i
    @n2 = gets.chomp.to_i
    @n1 - @n2 == @answer
    puts "The answer is... #{@answer}"
end

def multiplication_function
puts "Which numbers would you like to multiply?"
    @n1 = gets.chomp
    @n2 = gets.chomp
    @n1 * @n2 == @answer
    puts "The answer is... …
Run Code Online (Sandbox Code Playgroud)

ruby

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

ruby ×1