2013-03-24 74 views
-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); 
     } 
    } 
} 

當碰撞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(); 
     } 
    } 
} 
+7

1.編譯器錯誤** **不一樣例外。 2.你看到什麼錯誤? 3.你在哪裏看到它? – 2013-03-24 19:41:38

+1

什麼是錯誤?我注意到你有一個名字空間「撲克」和一個類「撲克」。這也會給你一個問題。嘗試改變命名。 – codingbiz 2013-03-24 19:44:14

+0

代碼編譯:在錯誤列表中說錯誤方法'playerOne'沒有重載需要0個參數。但是,當exe運行它說\t system.argumentOutOfRange – Kostadin 2013-03-24 19:45:01

回答

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); 
} 
+0

編譯但仍然崩潰並顯示一個exeption。 Poker.Player的類型初始值設定值可以豁免 – Kostadin 2013-03-24 20:00:40

+0

當某些事情中斷時,最有用的事情是知道爲什麼。當它中斷時彈出的消息將準確地告訴你問題是什麼。爲了讓我們能夠幫助你,你應該真的告訴我們這個消息是什麼意思。你會去看醫生,只是說「很痛」嗎? – 2013-03-24 20:03:57

+0

@KostadinAndonov public class Shuffle //:撲克 - 刪除撲克 – Daniil 2013-03-24 20:17:56

相關問題