2013-12-11 25 views
-4

好吧,讓我只是改變整個事情,並顯示我想要我的輸出到底是什麼,對不好的解釋抱歉。更改每個循環的變量數據

<table class="table table-bordered table-custom table-striped"> 
    <% 0.upto(years).each do |n| %> 
     <% @value = (((price*quantity)*percentage)/100)+(quantity*price) %> 
     <tr> 
     <td width="20%"><%= n %></td> 
     <td width="20%"><%= ((@value*percentage)/100)[email protected] %></td> 
     </tr> 
    <% end %> 
</table> 

我想弄清楚如何將@value變量設置爲從該循環中的一個calcalated中獲取的新值。

我的輸出ATM看起來是這樣的:

0 @value 

1 @value 

2 @value 

但我想它看起來像這樣:

0 @value 

1 new_value calculated from ((@value*percentage)/100)[email protected] 

2 new_value2 calculated from ((new_value*percentage)/100)+new_value 

我知道這仍然看起來像廢話,但我希望我試着這次解釋什麼即時通訊嘗試做; p

謝謝。

+1

你能澄清你所期望的輸出爲 – nickcen

回答

0

我想你想是這樣的:

<table class="table table-bordered table-custom table-striped"> 
    <% @value = price * quantity %> 
    <% 0.upto(years).each do |n| %> 
    <% @value += ((@value * percentage))/100 %> 
    <tr> 
     <td width="20%"><%= n %></td> 
     <td width="20%"><%= @value %></td> 
    </tr> 
    <% end %> 
</table> 

這會給你在價格上的增加(或減少)時,恆定比例加入每年。

+0

非常感謝你,這個工程完全按照我想要的,只有改+ =爲=,打印新@value不@值+舊@值所需要的,但你讓我的一天,謝謝!:D – user3091349

+0

woops,這是我的錯誤! – Slicedpan

0

使用減少而不是每個做中間值來累積。

<table class="table table-bordered table-custom table-striped"> 
    <% 0.upto(years).reduce(price * quantity) do |value, n| %> 
    <tr> 
     <td width="20%"><%= n %></td> 
     <td width="20%"><%= value %></td> 
    </tr> 
    <% value += ((value * percentage))/100 %> 
    <% end %> 
</table> 
+0

@LutzHorn感謝您的提醒。 – nickcen