+ 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"); } } } }
3 Réponses
+ 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).
+ 1
Thanks Zayn! 😁👍
0
You're welcome! 😁