0
Day of the week Koitlin
fun main(args: Array<String>) { val names = arrayOf(0, "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") var day = readLine()!!.toInt() when { day == 0 -> println("Invalid day") day == 1 -> println(names[1]) day == 2 -> println(names[2]) day == 3 -> println(names[3]) day == 4 -> println(names[4]) day == 5 -> println(names[5]) day == 6 -> println(names[6]) day == 7 -> println(names[7]) else -> println("Invalid day") } } It is saying Thursday should be Friday
5 Antworten
+ 3
The problem assume that Sunday is index 0, Monday is index 1 and so on... So it should be like this
fun main(args: Array<String>) {
val names = arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
var day = readLine()!!.toInt()
when {
day == 0 -> println(names[0])
day == 1 -> println(names[1])
day == 2 -> println(names[2])
day == 3 -> println(names[3])
day == 4 -> println(names[4])
day == 5 -> println(names[5])
day == 6 -> println(names[6])
// day == 7 -> println(names[7])
else -> println("Invalid day")
}
}
+ 1
It is now solved. Thanks.
0
fun main(args: Array<String>) {
val names = arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
var day = readLine()!!.toInt()
println(names[day])
}
Why this code doesnˋt work in all cases? I donˋt understand
0
Thomas Grauel you didn't put any statement for printing invalid day
Try this
fun main(args: Array<String>) {
val names = arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
var day = readLine()!!.toInt()
if(day<8)
println(names[day])
else
println("Invalid day")
}
0
Thanks thats the solition ! :) thanks