-4

我想将列表中的数字传递给 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();
        }
    }
}
4

1 回答 1

2

您有一个接受两个整数的方法,并且您尝试在不传递任何参数的情况下调用它:

public static void playerOne(int cardOne, int cardTwo)

但是你这样称呼它:

cards.playerOne();

你需要通读你的代码,看看它是如何工作的,也许阅读一些关于 C# 基础的文章?

我明白你想做什么。您想在方法中使用内部变量,但在方法的参数中包含它们。

public static void playerOne(int cardOne, int cardTwo)
{
    Console.WriteLine(cardOne);
    Console.WriteLine(cardTwo);
}

应该:

public static void playerOne()
{
    Console.WriteLine(cardOne);
    Console.WriteLine(cardTwo);
}
于 2013-03-24T19:44:18.297 回答