Kotlin:未解决的参考文献:totalFee
我只是在做这个小小的虚拟程序来练习,但是当试图将值打印到屏幕时,totalFee
没有得到解决。我已经看了一段时间了,无法弄清楚原因。
daysRented
是先前在程序中声明的int。
if (daysRented <= 10){
if(insPolicy == true){
val totalFee = (shortTermRate * daysRented) + (insRate * daysRented)
}
else{
val totalFee = shortTermRate * daysRented
}
}
else{
if(insPolicy == true){
val totalFee = (longTermRate * daysRented) + (insRate * daysRented)
}
else{
val totalFee = longTermRate * daysRented
}
}
println("Rental fee: $totalFee")
答案 0 :(得分:1)
Iterator
是一个表达式,所以我将其写为
if
真的应该改进到
val totalFee = if (daysRented <= 10){
if(insPolicy == true){ // can just be if (insPolicy)
(shortTermRate * daysRented) + (insRate * daysRented)
}
else{
shortTermRate * daysRented
}
}
else{
if(insPolicy == true){
(longTermRate * daysRented) + (insRate * daysRented)
}
else{
longTermRate * daysRented
}
}
甚至
val termRate = if (daysRented <= 10) shortTermRate else longTermRate
val totalFee =
if (insPolicy) {
(termRate * daysRented) + (insRate * daysRented)
}
else {
termRate * daysRented
}
答案 1 :(得分:0)
使用此代码。
val totalFee: Int
if (daysRented <= 10){
if(insPolicy){
totalFee = (shortTermRate * daysRented) + (insRate * daysRented)
}
else{
totalFee = shortTermRate * daysRented
}
}
else{
if(insPolicy){
totalFee = (longTermRate * daysRented) + (insRate * daysRented)
}
else{
totalFee = longTermRate * daysRented
}
}
println("Rental fee: $totalFee")