Witam, mam problem z moim kodem. Otóż kompilator nie wypluwa żadnych błędów ale gdy odpalam to w cmd zamiast programu pojawia się: "Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at test.Program.Main(String[] args) in D:\c#\test\Program.cs:line 25".Ktoś wie o co może chodzić?
KOD:
using System;
using System.Reflection.PortableExecutable;
namespace test
{
public class Wizard
{
public string Name;
public int Mana;
public string[] AvailableSpells = new string[3] {
"Spell #1, costs 20 mana",
"Spell #2, costs 50 mana",
"Spell #3, costs 100 mana",
};
}
class Program
{
static void Main(string[] args)
{
Wizard wizard = new Wizard();
wizard.Name = "Wizard #1";
wizard.AvailableSpells[1] = "Lumos Maxima";
wizard.AvailableSpells[2] = "Cruciatus";
wizard.AvailableSpells[3] = "Reducto";
wizard.Mana = 200;
Console.WriteLine("Hi " + wizard.Name + " You have " + wizard.Mana + " Mana");
Console.WriteLine("Choose your spell");
Console.WriteLine("Your Spells: ");
Console.WriteLine("1 - Lumos Maxima, costs 20 mana");
Console.WriteLine("2 - Cruciatus, costs 50 mana");
Console.WriteLine("3 - Reducto, costs 100 mana");
int Spell = Int32.Parse(Console.ReadLine());
switch (Spell)
{
case 1:
Console.WriteLine(wizard.Name + " casts a spell " + wizard.AvailableSpells[1]);
wizard.Mana -= 20;
break;
case 2:
Console.WriteLine(wizard.Name + " casts a spell " + wizard.AvailableSpells[2]);
wizard.Mana -= 50;
break;
case 3:
Console.WriteLine(wizard.Name + " casts a spell " + wizard.AvailableSpells[3]);
wizard.Mana -= 100;
break;
default:
Console.WriteLine("You didn't choose a right spell!");
break;
}
}
}
}