リストにある番号をPlayerクラスに渡して印刷したいのですが、それをコンパイルしようとすると、例外が発生します。私の間違いはどこにありますか?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Reflection;
namespace Game
{
public class Poker
{
public static void Main()
{
Player cards = new Player();
}
}
public class Shuffle : Poker
{
public static List<int> numbers = new List<int>();
Random random = new Random();
public Shuffle()
{
for (int runs = 0; runs < 530; runs++)
{
int number = random.Next(1, 53);
if (!numbers.Contains(number))
{
numbers.Add(number);
}
}
}
}
public class Player : Shuffle
{
private static int cardOne = numbers[0];
private static int cardTwo = numbers[1];
private static int cardThree = numbers[2];
private static int cardFour = numbers[3];
public static void playerOne()
{
Console.WriteLine(cardOne);
Console.WriteLine(cardTwo);
}
public static void playerTwo()
{
Console.WriteLine(cardThree);
Console.WriteLine(cardFour);
}
}
}
クラッシュが発生すると、次のように表示されます。クラッシュが発生すると、次のように表示されます。クラッシュが発生すると、次のように表示されます。
Unhandled Exception: System.TypeInitializationException: The type initializer fo
r 'Poker.Player' threw an exception. ---> System.ArgumentOutOfRangeException: In
dex was out of range. Must be non-negative and less than the size of the collect
ion.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Poker.Player..cctor() in c:\Users\Pc\Documents\Visual Studio 2012\Projects
\PokerProject\TestShit\ShuffleCards.cs:line 42
--- End of inner exception stack trace ---
at Poker.Player..ctor()
at Poker.Poker.Main() in c:\Users\Pc\Documents\Visual Studio 2012\Projects\Po
kerProject\TestShit\ShuffleCards.cs:line 15
Press any key to continue . . .
コードが修正されました。今すぐ実行してコンパイルします。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Reflection;
namespace Game
{
public class Poker
{
public static void Main()
{
Player cards = new Player();
cards.playerOne();
cards.playerTwo();
cards.playerThree();
cards.playerFour();
}
}
public class Shuffle : Poker
{
protected List<int> numbers = new List<int>();
Random random = new Random();
public Shuffle()
{
for (int runs = 0; runs < 530; runs++)
{
int number = random.Next(1, 53);
if (!numbers.Contains(number))
{
numbers.Add(number);
}
}
}
}
public class Player : Shuffle
{
private static int cardOne;
private static int cardTwo;
private static int cardThree;
private static int cardFour;
private static int cardFive;
private static int cardSix;
private static int cardSeven;
private static int cardEight;
public Player()
{
cardOne = numbers[0];
cardTwo = numbers[1];
cardThree = numbers[2];
cardFour = numbers[3];
cardFive = numbers[4];
cardSix = numbers[5];
cardSeven = numbers[6];
cardEight = numbers[7];
}
public void playerOne()
{
Console.Write("Player one cards: ");
Console.WriteLine(cardOne + " " + cardTwo);
Console.WriteLine();
}
public void playerTwo()
{
Console.Write("Player two cards: ");
Console.WriteLine(cardThree + " " + cardFour);
Console.WriteLine();
}
public void playerThree()
{
Console.Write("Player three cards: ");
Console.WriteLine(cardFive + " " + cardSix);
Console.WriteLine();
}
public void playerFour()
{
Console.Write("Player four cards: ");
Console.WriteLine(cardSeven + " " + cardEight);
Console.WriteLine();
}
}
}