+ 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
12 Answers
+ 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]);
}
}
}
}
+ 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
+ 1
I would have helped if i knew what was the initial code by sololearn and what you added.
+ 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
+ 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");
}
}
}
0
No problem, I already share the initial code
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();
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.
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");
}
}
}
}
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)
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])