调用方法时出现未处理的异常

时间:2013-03-24 19:40:27

标签: c# exception

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

1 个答案:

答案 0 :(得分: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);
}