我想將列表中的數字傳遞給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);
}
}
}
當碰撞occures它說:當碰撞occures它說:當碰撞occures它說:
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.編譯器錯誤** **不一樣例外。 2.你看到什麼錯誤? 3.你在哪裏看到它? – 2013-03-24 19:41:38
什麼是錯誤?我注意到你有一個名字空間「撲克」和一個類「撲克」。這也會給你一個問題。嘗試改變命名。 – codingbiz 2013-03-24 19:44:14
代碼編譯:在錯誤列表中說錯誤方法'playerOne'沒有重載需要0個參數。但是,當exe運行它說\t system.argumentOutOfRange – Kostadin 2013-03-24 19:45:01