Kotlin:多次调用时我无法保留函数的先前值
Kotlin: I am unable to hold previous value of a function when calling it multiple times
我需要在用户做出选择之前显示不同的座位安排,并在显示的座位安排中注册之前的选择。
我的代码是:
package cinema
fun main() {
println("Enter the number of rows:")
val row = readLine()!!.toInt()
println("Enter the number of seats in each row")
val seats = readLine()!!.toInt()
var price = 0
val totalSeats = row*seats
var rowNumberUpdate = 0
var seatNumberUpdate = 0
var a = true
fun seatDisplay(){
var newSeatCount = 1
println("Cinema: ")
print(" ")
while(newSeatCount <=seats){
print(" $newSeatCount")
newSeatCount += 1
}
print("\n")
for(i in 1..row){
print(i)
for(j in 1..seats){
if(i == rowNumberUpdate && j==seatNumberUpdate) print(" B") else print(" S")
}
println()
}
}
fun priceDisplay(){
println("Enter a row number: ")
val rowNumber = readln().toInt()
rowNumberUpdate = rowNumber
println("Enter a seat number in that row: ")
val seatNumber = readln().toInt()
seatNumberUpdate = seatNumber
if(totalSeats<=60){
price = 10
} else {
if(row%2==0){
if(rowNumber<=row/2) price = 10 else price = 8
} else {
if(rowNumber<=row/2) price = 10 else price = 8
}
}
println("Ticket price: $$price")
}
while(a){
println("1. Show the seats")
println("2. Buy a ticket")
println("0. Exit")
val optionsInput = readln().toInt()
when(optionsInput){
1 -> seatDisplay()
2 -> priceDisplay()
0 -> a = false
}
}
}
此代码的问题是,每次用户做出选择时,它都会显示最新选择的座位安排。它不保留之前选择的值。
您可以在作为 图像附加的输出中看到它。请缩放图像以清晰可见。
希望得到社区的一些帮助。
您将用户的选择存储在可以保存奇异值的变量 rowNumberUpdate
和 seatNumberUpdate
中。如果你打算记住所有的选择,你必须将这些变量更改为某种集合(例如列表)并将每个选择添加到该集合中:
val seatUpdate = mutableListOf<Pair<Int, Int>>()
并在 seatDisplay
中:
if(seatUpdate.contains(Pair(i,j))) print(" B") else print(" S")
并在 priceDisplay
中:
seatUpdate.add(Pair(rowNumber, seatNumber))
我需要在用户做出选择之前显示不同的座位安排,并在显示的座位安排中注册之前的选择。 我的代码是:
package cinema
fun main() {
println("Enter the number of rows:")
val row = readLine()!!.toInt()
println("Enter the number of seats in each row")
val seats = readLine()!!.toInt()
var price = 0
val totalSeats = row*seats
var rowNumberUpdate = 0
var seatNumberUpdate = 0
var a = true
fun seatDisplay(){
var newSeatCount = 1
println("Cinema: ")
print(" ")
while(newSeatCount <=seats){
print(" $newSeatCount")
newSeatCount += 1
}
print("\n")
for(i in 1..row){
print(i)
for(j in 1..seats){
if(i == rowNumberUpdate && j==seatNumberUpdate) print(" B") else print(" S")
}
println()
}
}
fun priceDisplay(){
println("Enter a row number: ")
val rowNumber = readln().toInt()
rowNumberUpdate = rowNumber
println("Enter a seat number in that row: ")
val seatNumber = readln().toInt()
seatNumberUpdate = seatNumber
if(totalSeats<=60){
price = 10
} else {
if(row%2==0){
if(rowNumber<=row/2) price = 10 else price = 8
} else {
if(rowNumber<=row/2) price = 10 else price = 8
}
}
println("Ticket price: $$price")
}
while(a){
println("1. Show the seats")
println("2. Buy a ticket")
println("0. Exit")
val optionsInput = readln().toInt()
when(optionsInput){
1 -> seatDisplay()
2 -> priceDisplay()
0 -> a = false
}
}
}
此代码的问题是,每次用户做出选择时,它都会显示最新选择的座位安排。它不保留之前选择的值。
您可以在作为
希望得到社区的一些帮助。
您将用户的选择存储在可以保存奇异值的变量 rowNumberUpdate
和 seatNumberUpdate
中。如果你打算记住所有的选择,你必须将这些变量更改为某种集合(例如列表)并将每个选择添加到该集合中:
val seatUpdate = mutableListOf<Pair<Int, Int>>()
并在 seatDisplay
中:
if(seatUpdate.contains(Pair(i,j))) print(" B") else print(" S")
并在 priceDisplay
中:
seatUpdate.add(Pair(rowNumber, seatNumber))