首页 > 解决方案 > Ruby:组合 if 语句:为什么我的代码总是打印相同的值?(例如:累进税制)

问题描述

我很糟糕,但出于实际原因我需要这个:为什么当“revenus”优于 15000 时我的代码总是打印“2500”?

这是关于累进税制的:我们给他一份收入,他给我们我们必须缴纳的税款。第 1 级:15 001 欧元以下不征收任何税款(不包括在内)。等级 2:15 001 至 4000 欧元(包括)之间的 10% 税。等级 3:40 001 至 80 000 欧元(含)之间的 15% 税。等级 3 税 25% 超过 80 000 欧元(不包括在内)。

由于 if 语句的组合,这可能是一些愚蠢的语法错误,如果您看到一些明显的错误,请告诉我。

revenus = 17305
copyrevenus = 17305
impots = 0

taux1 = 0/100
taux2 = 10/100
taux3 = 15/100
taux4 = 25/100

full1 = 0  #When the first scale is fully filled (superior or equal to the scale's max amount) i don't bother with multiplication, so this is for 15 000 * taux1 etc.
full2 = 2500
full3 = 6000 

if copyrevenus > 15000
 impots += full1
 revenus - 15000
 if copyrevenus > 40000
   impots += full2
   revenus - 25000
     if copyrevenus > 80000
       impots += full3
       revenus - 40000
       impots += revenus * taux4
     else
     impots += revenus * taux3
     end
 else
 impots += revenus * taux2
 end
else
 impots = revenus * taux1
end

puts impots

标签: rubyif-statementscaleprogressivemethod-combination

解决方案


Fravadona 和 mu 的回答太短:我修复了除法以使结果浮动,并且我的减法没有为“revenus”分配任何值,所以现在它已修复。

revenus = (write the amount)
fixrevenus = revenus
impots = 0

taux1 = 0.0/100.0
taux2 = 10.0/100.0
taux3 = 15.0/100.0
taux4 = 25.0/100.0

full1 = 0 
full2 = 2500
full3 = 6000 

if fixrevenus > 15000
 impots += full1
 revenus -= 15000
  
 if fixrevenus > 40000
   impots += full2
   revenus -= 25000
   
     if fixrevenus > 80000
       impots += full3
       revenus -= 40000
       impots += revenus * taux4
     else
     impots += revenus * taux3
     end
 
 else
 impots += revenus * taux2
 end

else
 impots += revenus * taux1
end

puts impots

推荐阅读