I need help with arrays in a exercise | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

I need help with arrays in a exercise

I need help with matrices. The problem description mentions the following: A game machine has 5 games installed. Write a program to take the number N as input and generate the corresponding game with index N of the matrix. If a user enters an invalid number that is outside the range of the array, the program should generate "Invalid number". Input example 3 Output example Puzzle. I tried to do it the way it is in the code that I shared but it does not work for me and it generates an error, I do not know if there is another way to make it simpler or in which I am failing. I appreciate your help. This si My Code: https://code.sololearn.com/cuSveym1SEdk/?ref=app

27th Jun 2021, 8:08 PM
Alex Narváez
Alex Narváez - avatar
12 Réponses
+ 8
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 = { "Alien Shooter", "Tic Tac Toe", "Snake", "Puzzle", "Football" }; //Your code goes here int gain = Convert.ToInt32(Console.ReadLine()); if (gain < 0 || 4 < gain) { Console.WriteLine("Invalid number"); } else { Console.WriteLine(games[gain]); } } } }
27th Jun 2021, 9:43 PM
visph
visph - avatar
+ 3
In python The answer is in the print function. games = [ 'Soccer', 'Tic Tac Toe', 'Snake', 'Puzzle', 'Rally'] #taking player's choice as a number input choice = int(input()) print(games[choice]) #python
2nd Oct 2023, 3:49 PM
Mohammad Salim
Mohammad Salim - avatar
+ 1
I would have helped if i knew what was the initial code by sololearn and what you added.
27th Jun 2021, 9:29 PM
Abhay
Abhay - avatar
+ 1
I thank you very much from my heart for the help you have given me. It is a very simple and short way to solve it, I am learning while I practice and this helps me to implement it in real projects. Thank you visph
27th Jun 2021, 10:56 PM
Alex Narváez
Alex Narváez - avatar
+ 1
using System; namespace SoloLearn { class Program { static void Main(string[] args) { string[] games = { "Alien Shooter", "Tic Tac Toe", "Snake", "Puzzle", "Football" }; //your code goes here try { Console.WriteLine(games[Int32.Parse(Console.ReadLine ())]); } catch { Console.WriteLine("Invalid number"); } } }
26th Feb 2023, 1:32 PM
Александр
0
No problem, I already share the initial code
27th Jun 2021, 9:31 PM
Alex Narváez
Alex Narváez - avatar
27th Jun 2021, 9:33 PM
Alex Narváez
Alex Narváez - avatar
0
string[] games = { "Alien Shooter", "Tic Tac Toe", "Snake", "Puzzle", "Football" }; int N = Convert.ToInt32(Console.ReadLine()); if (N >= games.Length) { Console.WriteLine("Invalid number"); } else { Console.WriteLine(games[N]); } Console.ReadKey();
28th Nov 2021, 2:14 PM
Даниил Герасимов
Даниил Герасимов - avatar
0
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 = { "Alien Shooter", "Tic Tac Toe", "Snake", "Puzzle", "Football" }; //your code goes here int N = Convert.ToInt32(Console.ReadLine()); if( N >= 0 && N <= 4) { Console.WriteLine(games[N]); } else { Console.WriteLine("Invalid number"); } } } } From the topics we have learned yet, I have passed all the test cases with the above solution. Thank you.
11th Jun 2023, 6:26 PM
Mohammed Safaf PT
Mohammed Safaf PT - avatar
0
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 = { "Alien Shooter", "Tic Tac Toe", "Snake", "Puzzle", "Football" }; try{ Console.Write(games[Int32.Parse(Console.ReadLine())]);} catch(Exception a) { Console.Write("Invalid number"); } } } }
11th Jul 2023, 2:46 PM
Mounir Charif
Mounir Charif - avatar
0
Using Python #installed games games = ['Soccer', 'Tic Tac Toe', 'Snake', 'Puzzle', 'Rally'] #taking player's choice as a number choice = int(input()) game = games[choice] #output the corresponding game print(game)
21st Oct 2023, 9:36 PM
Ashraf Bilal
Ashraf Bilal - avatar
0
#installed games games = [ 'Soccer', 'Tic Tac Toe', 'Snake', 'Puzzle', 'Rally'] #taking player's choice as a number input choice = int(input()) #output the corresponding game print(games[choice])
1st Mar 2024, 6:28 AM
Demilade Howells
Demilade Howells - avatar