条件循环重复打印错误的输出



我不知道如何表达标题"对不起"。基本上,我在写一个代码,画一个电影院和座位。该程序要求输入电影院的排数和座位数,并返回:电影院:

1 2 3 4 5 6 7 8 9 
1 S S S S S S S S S
2 S S S S S S S S S
3 S S S S S S S S S
4 S S S S S S S S S
5 S S S S S S S S S
6 S S S S S S S S S
7 S S S S S S S S S
8 S S S S S S S S S
9 S S S S S S S S S

然后,程序要求用户选择一排和一个座位,并应放置与上述相同的座位,但座位上有一个"B"标记:

1 2 3 4 5 6 7 8 9 
1 S S S S S S S S S
2 S S S S S S S S S
3 S S S S S S S S S
4 S S S B S S S S S
5 S S S S S S S S S
6 S S S S S S S S S
7 S S S S S S S S S
8 S S S S S S S S S
9 S S S S S S S S S

除非用户选择第1排和第1个座位,否则这是返回的座位:

1 2 3 4 5 6 7 8 9 
1 B S S S S S S S S
2 B S S S S S S S S
3 B S S S S S S S S
4 B S S S S S S S S
5 B S S S S S S S S
6 B S S S S S S S S
7 B S S S S S S S S
8 B S S S S S S S S
9 B S S S S S S S S

我确信这与代码末尾的for and if条件有关,但我不明白为什么它会重复打印"B"。我也知道我尝试了一种糟糕的方式,但我只想明白为什么我会有这个问题。这是完整的代码,所以你可以在你的IDEA:上进行测试

fun main(args: Array<String>) {

println("Enter the number of rows:")
val rows = readln().toInt()
println("Enter the number of seats in each row:")
val seats = readln().toInt()
val total = rows * seats
var s = 'S'
var cinemaLayout = mutableListOf<MutableList<Char>>()
val cinemaSeats = mutableListOf<Char>()

for (x in 1..seats) {
cinemaSeats.add(s)
}
for (x in 1..rows) {
cinemaLayout.add(cinemaSeats.toMutableList())
}
println("Cinema:")
print("  ")
for (x in 1..seats) {
print(x)
print(" ")
}
println()
var cleanLayout1 = " ${cinemaLayout[0].joinToString().replace("]", "n").replace("[", "").replace(",", "")}"
for (i in 1..rows) {
println("$i$cleanLayout1")
}
println("Enter a row number:")
val selectedRow = readln().toInt()
println("Enter a seat number in that row:")
val selectedSeat = readln().toInt()
if (total < 60) {
println("Ticket price: $10")
} else if (total > 60 && selectedRow % 2 === 0 && selectedRow <= rows / 2) {
println("Ticket price: $10")
} else {
println("Ticket price: $8")
}
var indexRow = selectedRow - 1
var indexSeat = selectedSeat - 1

cinemaLayout[indexRow][indexSeat] = 'B'
println("Cinema:")
print("  ")
for (x in 1..seats) {
print(x)
print(" ")
}
println()
for (i in 0 until rows) {
if (i === indexRow) {
println(
"${i + 1} ${
cinemaLayout[indexRow].joinToString().replace("]", "n").replace("[", "").replace(",", "")
}"
)
} else {
println(
"${i + 1} ${
cinemaLayout[0].joinToString().replace("]", "n").replace("[", "").replace(",", "")
}"
)
}
}
} 

这是因为您总是打印第一行,而不是indexRow

for (i in 0 until rows) {
if (i === indexRow) {
println("...cinemaLayout[indexRow]...")
} else {
println("...cinemaLayout[0]...")
}
}

因此,当i是您的目标行时,您将打印该行,否则您将打印行0,而不是i当前表示的行号。这意味着,当indexRow是第一行时,您每次只打印行0,这就是为什么您会看到它重复出现的原因。当第一行是而不是时,除了那一行之外,您仍在打印第0行的所有内容-当第一行发生变化时,情况会更明显。


您应该打印当前行而不是第一行:

} else {
// i not 0
println("...cinemaLayout[i]...")
}

但实际上,在这一点上,你为什么需要关心indexRow是什么?您的代码在这里的两种情况下都是相同的,并且您已经将'B'添加到了数据中——您可以按打印所有内容

for (i in 0 until rows) {
println("${i + 1} ${ cinemaLayout[i].joinToString().replace("]", "n").replace("[", "").replace(",", "") }")
}

或更好的

cinemaLayout.forEachIndexed { index, row ->
val seats = row.joinToString()
.replace("]", "n")
.replace("[", "")
.replace(",", "") 
println("${index + 1} $seats")
}

(还有更好的方法,比如用标准库替换——只是向你展示如何更干净地进行循环!(

这里的主要问题来自于代码的糟糕组织。您应该提取函数,并将业务逻辑与打印逻辑分离。

然后,无论我们检查的是什么i,您都可以更容易地注意到像打印cinemaLayout[0]的最后一行这样的事情。

此外,joinToString接受自变量,不必事后替换:joinToString(separator = "", prefix = "", postfix = "n")

最新更新