5

尝试使用新的 LearnStreet 在线教程学习 Ruby。

试图通过他们的问答系统获得帮助,但似乎没有人回答他们。

“现在可以在 account 对象上实现withdraw! 方法,它接受一个参数amount 并将余额减少指定的金额吗?定义方法后,继续从账户中提取100 美元并检查余额。”

是问题,我得到了两个提示

" 提示 1 代码 @balance = @balance - amount 减少 @balance 的金额。

提示 2 然后调用方法撤消!在帐户对象上 - account.withdraw!(100)。"

我的尝试是

def

account.widthdraw!

@balance = @balance - amount

end

account.withdraw!(100)

有什么我想念的想法吗?

4

3 回答 3

3

“现在可以在 account 对象上实现withdraw! 方法,它接受一个参数amount 并将余额减少指定的金额吗?定义方法后,继续从账户中提取100 美元并检查余额。”

一步一步来:

  • “你现在可以在账户对象上实现withdraw!方法吗?

    class Account
      def withdraw!
      end
    end
    
  • 它需要一个参数量...

    class Account
      def withdraw!(amount)
      end
    end
    
  • 并将余额减少指定金额?

    class Account
      def withdraw!(amount)
        @balance = @balance - amount
      end
    end
    
  • 确定方法后,继续从账户中提取 100 美元并检查余额。”

    account = Account.new
    account.withdraw!(100)
    
于 2012-11-05T01:13:53.777 回答
2

我想你会想要这样的东西。

class Account

    def withdraw! amount
         @balance -= amount
    end

end
于 2012-11-04T23:57:42.847 回答
0

这是这个问题的答案:

def account.withdraw!(amount)
    @balance = @balance - amount
end
account.withdraw!(100)
于 2012-12-28T13:37:43.773 回答