+ 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 ........

1st May 2021, 9:34 AM
Abhishek Biswas
Abhishek Biswas - avatar
3 Réponses
+ 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
1st May 2021, 10:38 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 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") } }
31st Oct 2021, 2:12 PM
Oussama Merroun
Oussama Merroun - avatar
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") }
15th Apr 2024, 3:45 PM
Anik Karmakar
Anik Karmakar - avatar