好的,首先我想說我是一個新的程式員,我是自學的。我目前正在考慮如何制作一個程式(我正在使用 Kotlin)來計算給定值中的季度數,并說明剩余的變化。
所以如果我輸入 input: 1.45
輸出會告訴我:
5個季度
0.20 變化
現在,我已經對 void 方法進行了研究,但我仍然對它的外觀感到困惑。有沒有人愿意幫助我了解 id 是如何開始的?
我的意思是按美元計算的四分之一。因此,如果您有 1.40 美元 / 0.25 美元(季度)= 5 個季度和 0.20 美元。所以我正在嘗試制作一些可以計算貨幣的東西。我已經有一個計算數學的弱代碼,但我需要在 2 個不同的輸出行上分開的季度數和剩余的變化。
uj5u.com熱心網友回復:
您可以println將每個輸出放在單獨的行上
fun main() {
change(1.25)
changeWithNames(1.25)
}
fun change(x: Double) {
println(x.div(0.25).toInt()) // toInt to cut off the remainder
println(x.rem(0.25).toFloat()) // toFloat so that it rounds to the nearest hundreds place
}
fun changeWithNames(x: Double) {
println("${"%s".format(x.div(0.25).toInt())} quarters")
println("${"%.2f".format(x.rem(0.25))} Change")
}
uj5u.com熱心網友回復:
fun main() {
val input = "1.45"
// here you convert the input into minor units, e.g. $1.45 => 145 cents
val total = (input.toFloat() * 100).toInt()
// here you get whole number of quarters
val quartersCount = total / 25
// here you get the change and convert it back to major units (20 cents => $0.2)
val change = (total - 25 * quartersCount) / 100f
println("$quartersCount quarters")
println("${"%.2f".format(change)} Change") // bit more complex just to keep 2 decimal places
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371516.html
上一篇:嘗試以編程方式均勻間隔網格布局
