不能识别循环

时间:2016-06-23 20:17:23

标签: c# for-loop

我正在制作一个简单的基于文本的游戏,但是当我尝试编译它时,它会在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);
    }
}

}

如果有人知道如何解决这个问题,请分享。

3 个答案:

答案 0 :(得分:2)

您的第一个循环不是此部分方法的一部分:for (int x=0; x<9; x++) ...

创建了一个方法并将循环插入其中

答案 1 :(得分:2)

您必须实现Board class 构造函数

  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 :(得分: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]);
                }
            }
    }
    }
}