2009-11-09 181 views
0

我正在構建大酒杯和主要類的遊戲有問題。這是我的代碼:「成員修飾符」靜態「必須在成員類型和名稱之前」錯誤C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public class blackjack 
    { 

     static string[] playercards = new string[11]; 
     static string hitstay = ""; 
     static int total = 0, count = 1, dealertotal = 0; 
     static Random cardshuffler = new Random(); 
     static Form1 f1 = new Form1(); 
     Form1.ControlCollection 



     static void start() 
     { 
      dealertotal = cardshuffler.Next(15, 22); 
      playercards[0] = deal(); 
      playercards[1] = deal(); 

      bj(); 
     } 


     private static void hit() 
     { 

      count += 1; 
      playercards[count] = deal(); 
      f1.textBox2("you were dealed a(n) " + playercards[count] + ".your new total is " + total + "."); 
      if (total.Equals(21)) 
      { 
       f1.textBox2("you got blackjack! the dealer's total was " + dealertotal + ".would you like to play again?"); 
       playagain(); 
      } 
      else if (total > 21) 
      { 
       f1.textBox2("you busted, therefore you lost. sorry. the dealer's total was " + dealertotal + ".would you like to play again? y/n"); 
       playagain(); 
      } 
      else if (total < 21) 
      { 
       do 
       { 
        f1.textBox2("would you like to hit or stay?"); 
        hitstay = Console.ReadLine(); 
       } while (!hitstay.Equals("hit") && !hitstay.Equals("stay")); 
       bj(); 
      } 
     } 

     private static string deal() 
     { 
      string Card = ""; 
      int cards = cardshuffler.Next(1, 14); 
      switch (cards) 
      { 
       case 1: Card = "Two"; total += 2; 
        break; 
       case 2: Card = "Three"; total += 3; 
        break; 
       case 3: Card = "Four"; total += 4; 
        break; 
       case 4: Card = "Five"; total += 5; 
        break; 
       case 5: Card = "Six"; total += 6; 
        break; 
       case 6: Card = "Seven"; total += 7; 
        break; 
       case 7: Card = "Eight"; total += 8; 
        break; 
       case 8: Card = "Nine"; total += 9; 
        break; 
       case 9: Card = "Ten"; total += 10; 
        break; 
       case 10: Card = "Jack"; total += 10; 
        break; 
       case 11: Card = "Queen"; total += 10; 
        break; 
       case 12: Card = "King"; total += 10; 
        break; 
       case 13: Card = "Ace"; total += 11; 
        break; 

      } 
      return Card; 
     } 

     static void bj() 
     { 
      if (hitstay.Equals("hit")) 
      { 
       hit(); 
      } 
      else if (hitstay.Equals("stay")) 
      { 
       if (total > dealertotal && total <= 21) 
       { 
        Form1.textBox2.show("you won! the dealer busted with " + dealertotal + " as their total" + "your total was " + total); 
        playagain(); 
       } 
       else if (total < dealertotal) 
       { 
        Form1.textBox2.show("sorry, you lost! the dealer's total was " + dealertotal); 
        playagain(); 
       } 

      } 
     } 

     private static void playagain() 
     { 



     } 
    } 
} 

的錯誤是在第21行,列9

+0

有人請修復代碼標記。 – okutane 2009-11-09 03:57:10

+0

哪一行是21行?它可能更好地工作,只顯示導致問題的相關代碼片段。 – Andy 2009-11-09 03:57:29

+0

你可以修復你的代碼(其中一些沒有正確縮進),並突出顯示錯誤發生的地方嗎?你真的不希望人數:) – 2009-11-09 03:58:49

回答

4

的錯誤實際上是在第17行 - 這個標識符懸掛晾乾:

Form1.ControlCollection 

無論是你的意思是宣佈一個領域或這個代碼不應該在那裏。此代碼稍後會導致錯誤,因爲解析器正在尋找一個字段名稱,並將其放在那裏,並假定您的static Main方法是與它一起使用的標識符。

+3

這是錯誤,但問題是更大: – leppie 2009-11-09 04:00:18

+0

謝謝,但現在我似乎有另一個問題。我現在得到六個錯誤,看起來像這樣: 非可調用成員'WindowsFormsApplication1.Form1.textBox2'我有4個這樣的問題 不可使用的成員'WindowsFormsApplication1.Form1.textBox2'不能像方法一樣使用 我有2個,請幫助。 – David 2009-11-09 04:02:57

1
static Form1 f1 = new Form1(); 
    Form1.ControlCollection // What is this? You forgot to write something there? 



    static void start() 

編譯器認爲有一個以「Form1.ControlCollection」開頭並繼續「static void start()」的聲明。