示例,使用 Kotlin 计算价格 100 分别涨跌幅 10% 后的分段价格。
示例代码
prices.kt
data class PercentPrice(val percent: Int, val price: Double)
fun main(args: Array<String>) {
println("Start!")
val price = 100.0
val prices = mutableListOf<PercentPrice>()
for (i in 1..10 step 1) {
prices.add(PercentPrice(i, price * (1.0 + i/100.0)))
prices.add(PercentPrice(-i, price * (1.0 - i/100.0)))
}
for (item in prices) {
println("%d%% - %.2f".format(item.percent, item.price))
}
}
执行结果
> kotlinc prices.kt -include-runtime -d prices.jar
> java -jar prices.jar
Start!
1% - 101.00
-1% - 99.00
2% - 102.00
-2% - 98.00
3% - 103.00
-3% - 97.00
4% - 104.00
-4% - 96.00
5% - 105.00
-5% - 95.00
6% - 106.00
-6% - 94.00
7% - 107.00
-7% - 93.00
8% - 108.00
-8% - 92.00
9% - 109.00
-9% - 91.00
10% - 110.00
-10% - 90.00
微信关注我哦 👍
我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式