Ruby:组合if语句:为什么我的代码总是打印相同的值?(例如:累进税率表)
我很糟糕,但我需要这一点是出于实际原因:为什么我的代码总是打印“2500”,而“revenus”优于15000 这是关于累进税的规模:我们给他一笔收入,他给我们必须支付的税。 等级1:不包括任何低于15 001欧元的税费(不包括在内)。 等级2:15 001至4000欧元(含)之间的10%税收。 等级3:40 001至80 000欧元(含)之间的15%税收。 第3级税率25%,高于80000欧元(不包括在内)Ruby:组合if语句:为什么我的代码总是打印相同的值?(例如:累进税率表),ruby,if-statement,scale,progressive,method-combination,Ruby,If Statement,Scale,Progressive,Method Combination,我很糟糕,但我需要这一点是出于实际原因:为什么我的代码总是打印“2500”,而“revenus”优于15000 这是关于累进税的规模:我们给他一笔收入,他给我们必须支付的税。 等级1:不包括任何低于15 001欧元的税费(不包括在内)。 等级2:15 001至4000欧元(含)之间的10%税收。 等级3:40 001至80 000欧元(含)之间的15%税收。 第3级税率25%,高于80000欧元(不包括在内) 这可能是一些愚蠢的语法错误,因为if语句的组合,如果您看到一些明显的错误,请告诉我 r
这可能是一些愚蠢的语法错误,因为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
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
法国税哈哈。你的
taux
是错误的,因为你使用的是整数除法。我会马上检查其余的。要么revenus-15000
要么revenus-40000
更改任何内容,他们只需执行减法并丢弃结果。也许你想要revenus-=15000
和revenus-=40000
相反?“[W]为什么当‘revenus’优于15000时,我的代码总是打印“2500”?它不打印“0”(按原样)。当revenus大于40000且小于或等于80000时,它会打印“2500”OK是的,这是明显的盲。。。谢谢我修正了整数和-=赋值,现在它适用于所有刻度。。。谢谢你们!“这可能是一些愚蠢的语法错误”——它不可能是语法错误,因为如果是语法错误,代码甚至无法解析,更不用说运行了。事实上,即使结果是错误的,也会得到任何结果,这已经表明不可能是语法错误。