2013-02-07 37 views
-4
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Test 
{ 
    class Program1 
    { 
    static void Main(string[] args) 
    { 
     public static char[,] board = new char[3,3]; //3x3 board game 
     public static char? NULL = null;    //Null character 
     public static const int COL = 3;    //Max # of columns 
     public static const int ROW = 3;    //Max # of rows 
     void drawBoard(); 
    } 

    public void drawBoard()        //Method that draws board 
    { 
     for(int x = 0; ROW < 3; x++)           
     { 
      Console.WriteLine("-------------"); 
      for(int y = 0; COL < 3; y++) 
      { 
       Console.Write("|"); 
       if(board[i,j] == NULL) 
       { 
        Console.Write(" "); 
       } 
       else 
       { 
        Console.Write(" " + board[i,j] + " "); 
       } 
      } 
      Console.WriteLine("|"); 
     } 
     Console.WriteLine("-------------"); 
    } 
    } 
} 

我正在嘗試編寫一個井字遊戲。我很新(從幾個小時前開始學習)到C#,所以我不明白爲什麼我的代碼包含錯誤,並且不會編譯。另外,如果可能的話,你能解釋一下在C#中使用命名空間的全部目的嗎?我不太明白名字空間會有什麼不同。提前感謝您的時間和協助。錯誤缺少一個括號,而所有都存在(C#錯誤)

+6

你在使用編譯的「發現的錯誤」的意見(可能錯過了一些)?視覺工作室?如果是這樣,它非常擅長告訴你*正是錯誤所在。如果你不使用VS,爲什麼不呢? –

+0

你從哪裏得到錯誤?什麼是確切的錯誤信息?拋出一堆代碼,並在沒有告訴我們**的情況下說「我得到一個錯誤」**在哪裏**是不恰當的 - 編譯器告訴你它認爲哪一行有問題,所以沒有理由不應該將這些信息傳遞給給我們。 (另外,僅供將來參考:這裏的一般規則是每個帖子有一個問題,因此要求一個完全獨立的問題,並說「雖然你在...」並不太合適。) –

+1

@sgeddes - The上面的代碼仍然格式不正確。這個問題與缺失大括號無關。 –

回答

4

這就是問題所在:

static void Main(string[] args) 
{ 
    public static char[,] board = new char[3,3]; //3x3 board game 
    public static char? NULL = null;    //Null character 
    public static const int COL = 3;    //Max # of columns 
    public static const int ROW = 3;    //Max # of rows 
    void drawBoard(); 
} 

你想一個方法聲明中領域public static變量)。你不能那樣做。您只能在方法中聲明本地變量。

同樣這樣的:

void drawBoard(); 

是不是你調用一個方法。你調用它是這樣的:

drawBoard(); 

void將是聲明的方法的一部分 - 而不是調用。

你也試圖調用實例方法沒有一個實例來調用它 - 這是行不通的。也許drawBoard應該是一個靜態方法?

基本上,我建議你從簡單的事情開始,並儘可能經常地編譯你的代碼 - 這樣你就不會積累破碎的積壓。

1

這麼多錯誤!

這裏是一個「編譯」的版本(這並不意味着「工作」),與

using System; 


namespace Test 
{ 
    internal class Program1 
    { 
//don't declare your variables in a method body 
     public static char[,] board = new char[3,3]; 
     public static char? NULL = null; 
     public const int COL = 3; //you can't have static const together, choose one 
     public const int ROW = 3; //same 

     private static void Main(string[] args) 
     { 
      drawBoard();//call to method without return type 
     } 

     public static void drawBoard() //method must be static if you want to call it from a static context 
     { 
      for (int x = 0; x < ROW; x++)//ROW < 3 always false 
      { 
       Console.WriteLine("-------------"); 
       for (int y = 0; y < COL; y++)//COL < 3 always false 
       { 
        Console.Write("|"); 
        if (board[x, y] == NULL)//i, j don't exists 
        { 
         Console.Write(" "); 
        } 
        else 
        { 
         Console.Write(" " + board[x, y] + " ");//i, j don't exist 
        } 
       } 
       Console.WriteLine("|"); 
      } 
      Console.WriteLine("-------------"); 
     } 
    } 
} 
+0

我不明白的意見'/ /行<3總是假'和'//COL <3總是虛假的。兩者都高於0,因此兩個循環都將開始並結束迭代。 –

+0

@TimSchmelter'const int ROW = 3' =>'3 <3',所以你永遠不會進入循環...不要認爲這是我們想要的...... –

+0

@Rahael:但循環變量是'x '和'y'也增加了,最初低於最大值'ROW'和'COL'(或者我錯過了一些明顯的東西?)。 –