Can someone explain to me why code coach did not accept this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can someone explain to me why code coach did not accept this?

Why didn't it work? I'm pretty sure that's correct: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string[] games = new string [5]{ "Alien Shooter", "Tic Tac Toe", "Snake", "Puzzle", "Football" }; //your code goes here int x = Convert.ToInt32(Console.ReadLine()); if (x < games.Length) { Console.WriteLine(games[x]); } else if (x > games.Length) { Console.WriteLine("Invalid number"); } } } }

2nd Apr 2023, 4:31 AM
Gradi Maxime Kasita Mbatika
3 Answers
+ 4
the else if condition should be: else if (x >= games.Length){ ... } since the length itself is also an invalid index for the array (because as you know the index start at 0).
2nd Apr 2023, 4:50 AM
Zayn
+ 1
Thanks Zayn! 😁👍
2nd Apr 2023, 4:55 AM
Gradi Maxime Kasita Mbatika
0
You're welcome! 😁
2nd Apr 2023, 6:01 AM
Zayn