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

28th Nov 2021, 3:59 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
5 Answers
+ 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") } }
28th Nov 2021, 4:33 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 1
It is now solved. Thanks.
28th Nov 2021, 7:30 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
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
5th Jan 2022, 8:27 PM
Thomas Grauel
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") }
6th Jan 2022, 3:18 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
0
Thanks thats the solition ! :) thanks
6th Jan 2022, 5:42 AM
Thomas Grauel