2016-06-23 104 views
0

我正在製作一個簡單的基於文本的遊戲,但是當我嘗試編譯它時,它在for循環中給了我一個錯誤。它不承認它,並給我一個錯誤的單詞和結束括號。For循環無法識別

using System; 
namespace ThreeDGame { 
class Player { 
    private int px=1; 
    private int py=1; 
    private int pz=1; 
    public Player(int x, int y, int z) { 
     px=x; 
     py=y; 
     pz=z; 
    } 
    public void xp() { 
     if (++px=10) { 
      px=1; 
     } 
    } 
    public void xn() { 
     if (--px=0) { 
      px=9; 
     } 
    } 
    public void yp() { 
     if (++py=10) { 
      py=1; 
     } 
    } 
    public void yn() { 
     if (--py=0) { 
      py=9; 
     } 
    } 
    public void zp() { 
     if (++pz=10) { 
      pz=1; 
     } 
    } 
    public void zn() { 
     if (--pz=0) { 
      pz=9; 
     } 
    } 
} 
class Board { 
    string[] board = new string[9,9,9]; 
    for (int x=0; x<9; x++) { 
     for (int y=0; y<9; y++) { 
      for (int z=0; z<9; z++) { 
       board[x,y,z] = "~"; 
      } 
     } 
    } 
    public void DispBoard(int z) { 
     for (int a=1; a>=9; a++) { 
      Console.WriteLine(); 
      for (int b=1; b>=9; b++) { 
       Console.Write(board[a-1,b-1,z-1]); 
      } 
     } 
    } 
} 
class Game { 
    static void Main() { 
     Board b = new Board(); 
     b.DispBoard(1); 
    } 
} 

}

如果有人知道如何解決這個問題,請大家分享。

+1

在下面的答案除了錯誤解釋,在Dispboard代碼永遠不會做你認爲它應該做的。 _int a = 1; a> = 9; _? – Steve

+0

Visual Studio在您的代碼中顯示一些其他錯誤:問題:++ px = 10,新字符串[9,9,9]; –

回答

2

您必須實現Board構造

class Board { 
    //DONE: string[,,] - you want a 3d array, right? 
    string[,,] board = new string[9, 9, 9]; 

    //DONE: you can't just run loop within the class, but within a constructor (or method) 
    public Board() { 
     //DONE: do not use magic values (9), but GetLength(dimension) 
     for (int x = 0; x < board.GetLength(0); ++x) 
     for (int y = 0; y < board.GetLength(1); ++y) 
      for (int z = 0; z < board.GetLength(2); ++z) 
      board[x, y, z] = "~"; 
    } 

    public void DispBoard(int z) { 
     //DONE: validate public methods' values 
     if ((z < 1) || (z > board.GetLength(2))) 
     throw new ArgumentOutOfRangeException("z"); 

     //DONE: wrong condition ">= 9" changed into right one "< board.GetLength(0)" 
     for (int x = 0; x < board.GetLength(0); ++x) { 
     Console.WriteLine(); 

     //DONE: wrong condition ">= 9" changed into right one "< board.GetLength(1)" 
     for (int y = 0; y < board.GetLength(1); ++y) 
      Console.Write(board[x, y, z - 1]); 
     } 
    } 
    } 
2

你的第一個循環是不是方法的一部分,這一部分:for (int x=0; x<9; x++) ...

創建的方法和插入環插入它

+0

可能代碼應該進入董事會的構造函數和板陣列聲明爲 – Steve

+0

類的實例變量我認爲,應該是ClearBoard方法的一部分會更好。但我同意它也應該在構造函數中調用。 –

0

您可以創建一個承包商,並調用循環的方法。

class Board { 
    public Board(){ 
     dosometing(); 
    } 
    public void dosometing(){ 
      string[] board = new string[9,9,9]; 
     for (int x=0; x<9; x++) { 
      for (int y=0; y<9; y++) { 
       for (int z=0; z<9; z++) { 
        board[x,y,z] = "~"; 
       } 
      } 
     } 
     public void DispBoard(int z) { 
      for (int a=1; a>=9; a++) { 
       Console.WriteLine(); 
       for (int b=1; b>=9; b++) { 
        Console.Write(board[a-1,b-1,z-1]); 
       } 
      } 
    } 
    } 
}