+ 3
kotlin question Array ......
Day of the Week The given program declares an array of weekday names. You need to take a number as input and output the name of the day at that index. In case the input is out of the range, output "Invalid day". Sample Input: 2 Sample Output: Tuesday here is my Answer fun main(args: Array<String>) { val names = arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") val day = readLine()!!.toInt() if (day >= 0) { println(names[day]) } } it passed all the test except the Fifth Test...... please help me to fix this ........
3 ответов
+ 4
Hi! you don't have a valid input check for a given range, and what should your program output if the data is out of bounds? read the task carefully again
+ 1
#try this:
fun main(args: Array<String>) {
val names = arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
var day = readLine()!!.toInt()
if(day <= 6 && day >= 0) {
println(names[day]) }
else {
println("Invalid day")
}
}
0
fun main() {
println("Enter a number of days: ")
val day = readln().toInt()
val days = arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
if (day <days.size)
println(days[day])
else println("Index out of bound")
}