简体   繁体   English

C# 使用未赋值的变量?

[英]C# use of unassigned variable?

So basically I have a class for a TicTacToe game and a derived class just to practice using inheritance.所以基本上我有一个用于 TicTacToe 游戏的类和一个派生类,只是为了练习使用继承。 The class has several methods and all should work perfectly fine but in the main function, when I finally make three objects of the derived class I get three errors "use of unassigned local variable 'boardOne'" "use of unassigned local variable 'boardTwo'" and "use of unassigned local variable 'boardThree'".该类有几种方法,所有方法都应该可以正常工作,但是在主函数中,当我最终创建派生类的三个对象时,出现三个错误“使用未分配的局部变量‘boardOne’”“使用未分配的局部变量‘boardTwo’ ”和“使用未分配的局部变量‘boardThree’”。 I do not understand why this is, they are objects, not variables.我不明白为什么会这样,它们是对象,而不是变量。

    public class TicTacToe
    {
        protected char[] boardCells = new char[9];
        public int boardSpacesUsed;
        public TicTacToe()                            //constructor
        {
            boardCells[0] = '1';
            boardCells[1] = '2';
            boardCells[2] = '3';
            boardCells[3] = '4';
            boardCells[4] = '5';
            boardCells[5] = '6';
            boardCells[6] = '7';
            boardCells[7] = '8';
            boardCells[8] = '9';
            boardCells[9] = '\0';
            int boardSpacesUsed = 0;
        }
        public void playerOneMove()
        {
            bool space = false;
            char cell = '\0';
            while (space == false)
            {
                Console.WriteLine("Please enter cell number you wish to mark: ");
                cell = Convert.ToChar(Console.ReadLine());
                switch (cell)
                {
                    case '1':
                        if (boardCells[0] == 'X' || boardCells[0] == 'O')
                        {
                            Console.WriteLine("Illegal Move");
                        }
                        else
                        {
                            boardCells[0] = 'X';
                            space = true;
                        }
                        break;
                    case '2':
                        if (boardCells[1] == 'X' || boardCells[1] == 'O')
                            Console.WriteLine("Illegal Move");
                        else
                        {
                            boardCells[1] = 'X';
                            space = true;
                        }
                        break;
                    case '3':
                        if (boardCells[2] == 'X' || boardCells[2] == 'O')
                            Console.WriteLine("Illegal Move");
                        else
                        {
                            boardCells[2] = 'X';
                            space = true;
                        }
                        break;
                    case '4':
                        if (boardCells[3] == 'X' || boardCells[3] == 'O')
                            Console.WriteLine("Illegal Move");
                        else
                        {
                            boardCells[3] = 'X';
                            space = true;
                        }
                        break;
                    case '5':
                        if (boardCells[4] == 'X' || boardCells[4] == 'O')
                            Console.WriteLine("Illegal Move");
                        else
                        {
                            boardCells[4] = 'X';
                            space = true;
                        }
                        break;
                    case '6':
                        if (boardCells[5] == 'X' || boardCells[5] == 'O')
                            Console.WriteLine("Illegal Move");
                        else
                        {
                            boardCells[5] = 'X';
                            space = true;
                        }
                        break;
                    case '7':
                        if (boardCells[6] == 'X' || boardCells[6] == 'O')
                            Console.WriteLine("Illegal Move");
                        else
                        {
                            boardCells[6] = 'X';
                            space = true;
                        }
                        break;
                    case '8':
                        if (boardCells[7] == 'X' || boardCells[7] == 'O')
                            Console.WriteLine("Illegal Move");
                        else
                        {
                            boardCells[7] = 'X';
                            space = true;
                        }
                        break;
                    case '9':
                        if (boardCells[8] == 'X' || boardCells[8] == 'O')
                            Console.WriteLine("Illegal Move");
                        else
                        {
                            boardCells[8] = 'X';
                            space = true;
                        }
                        break;
                    default:
                        Console.WriteLine("Cell Does NOT Exist!");
                        break;
                }// end of switch statement
            }//end of while loop
            boardSpacesUsed++;
        }// end of playerOneMove();
        public void CPUMove()                                                   //method marks cell for CPU
        {
            int iCell = 0;
            bool space = false;
            while (space == false)
            {
                Random rand = new Random();
                iCell = rand.Next(1, 9);
                switch (iCell)                                              //switch statement to mark the cell
                {
                    case 1:
                        if (boardCells[0] == 'X' || boardCells[0] == 'O')
                        {
                            space = false;
                        }
                        else
                        {
                            boardCells[0] = 'O';
                            space = true;
                        }
                        break;
                    case 2:
                        if (boardCells[1] == 'X' || boardCells[1] == 'O')
                            space = false;
                        else
                        {
                            boardCells[1] = 'O';
                            space = true;
                        }
                        break;
                    case 3:
                        if (boardCells[2] == 'X' || boardCells[2] == 'O')
                            space = false;
                        else
                        {
                            boardCells[2] = 'O';
                            space = true;
                        }
                        break;
                    case 4:
                        if (boardCells[3] == 'X' || boardCells[3] == 'O')
                            space = false;
                        else
                        {
                            boardCells[3] = 'O';
                            space = true;
                        }
                        break;
                    case 5:
                        if (boardCells[4] == 'X' || boardCells[4] == 'O')
                            space = false;
                        else
                        {
                            boardCells[4] = 'O';
                            space = true;
                        }
                        break;
                    case 6:
                        if (boardCells[5] == 'X' || boardCells[5] == 'O')
                            space = false;
                        else
                        {
                            boardCells[5] = 'O';
                            space = true;
                        }
                        break;
                    case 7:
                        if (boardCells[6] == 'X' || boardCells[6] == 'O')
                            space = false;
                        else
                        {
                            boardCells[6] = 'O';
                            space = true;
                        }
                        break;
                    case 8:
                        if (boardCells[7] == 'X' || boardCells[7] == 'O')
                            space = false;
                        else
                        {
                            boardCells[7] = 'O';
                            space = true;
                        }
                        break;
                    case 9:
                        if (boardCells[8] == 'X' || boardCells[8] == 'O')
                            space = false;
                        else
                        {
                            boardCells[8] = 'O';
                            space = true;
                        }
                        break;
                }
            }
            boardSpacesUsed++;
        }
        public void getBoardCells()
        {
            Console.WriteLine("  " + " " + "  |  " + " " + "  |  " + " ");
            Console.WriteLine("  " + boardCells[0] + "  |  " + boardCells[1] + "  |  " + boardCells[2]);
            Console.WriteLine("__" + "_" + "__|__" + "_" + "__|__" + "_");
            Console.WriteLine("  " + " " + "  |  " + " " + "  |  " + " ");
            Console.WriteLine("  " + boardCells[3] + "  |  " + boardCells[4] + "  |  " + boardCells[5]);
            Console.WriteLine("__" + "_" + "__|__" + "_" + "__|__" + "_");
            Console.WriteLine("  " + " " + "  |  " + " " + "  |  " + " ");
            Console.WriteLine("  " + boardCells[6] + "  |  " + boardCells[7] + "  |  " + boardCells[8]);
            Console.WriteLine("  " + " " + "  |  " + " " + "  |  " + " ");
        }
        public bool playerOneWinCheck(ref int score)
        {
            bool check = false;
            if (boardCells[0] == 'X' && boardCells[1] == 'X' && boardCells[2] == 'X')
            {
                check = true;
                score++;
            }
            if (boardCells[3] == 'X' && boardCells[4] == 'X' && boardCells[5] == 'X')
            {
                check = true;
                score++;
            }
            if (boardCells[6] == 'X' && boardCells[7] == 'X' && boardCells[8] == 'X')
            {
                check = true;
                score++;
            }
            if (boardCells[0] == 'X' && boardCells[3] == 'X' && boardCells[6] == 'X')
            {
                check = true;
                score++;
            }
            if (boardCells[1] == 'X' && boardCells[4] == 'X' && boardCells[7] == 'X')
            {
                check = true;
                score++;
            }
            if (boardCells[2] == 'X' && boardCells[5] == 'X' && boardCells[8] == 'X')
            {
                check = true;
                score++;
            }
            if (boardCells[0] == 'X' && boardCells[4] == 'X' && boardCells[8] == 'X')
            {
                check = true;
                score++;
            }
            if (boardCells[6] == 'X' && boardCells[4] == 'X' && boardCells[2] == 'X')
            {
                check = true;
                score++;
            }
            if (check == true)
                return true;
            else
                return false;
        }
        public bool CPUWinCheck(ref int score)                                                          //Method to check to see if CPU won INCRAMENTS SCORE UP ONE IF ANYTHING HOLDS TRUE
        {
            bool check = false;
            if (boardCells[0] == 'O' && boardCells[1] == 'O' && boardCells[2] == 'O')
            {
                check = true;
                score++;
            }
            if (boardCells[3] == 'O' && boardCells[4] == 'O' && boardCells[5] == 'O')
            {
                check = true;
                score++;
            }
            if (boardCells[6] == 'O' && boardCells[7] == 'O' && boardCells[8] == 'O')
            {
                check = true;
                score++;
            }
            if (boardCells[0] == 'O' && boardCells[3] == 'O' && boardCells[6] == 'O')
            {
                check = true;
                score++;
            }
            if (boardCells[1] == 'O' && boardCells[4] == 'O' && boardCells[7] == 'O')
            {
                check = true;
                score++;
            }
            if (boardCells[2] == 'O' && boardCells[5] == 'O' && boardCells[8] == 'O')
            {
                check = true;
                score++;
            }
            if (boardCells[0] == 'O' && boardCells[4] == 'O' && boardCells[8] == 'O')
            {
                check = true;
                score++;
            }
            if (boardCells[6] == 'O' && boardCells[4] == 'O' && boardCells[2] == 'O')
            {
                check = true;
                score++;
            }
            if (check == true)
                return true;
            else
                return false;
        }
        ~TicTacToe()
        {
            for (int c = 0; c <= 10; c++)
            {
                boardCells[c] = '\0';

            }
            boardSpacesUsed = 0;
        }
    }
    public class ThreeD : TicTacToe
    {

        public void threeDWinCheck(ThreeD boardOne, ThreeD boardTwo, ThreeD boardThree, ref int score) //new function to check to see ThreeD wins
        {
            if (boardOne.boardCells[0] == 'X' && boardTwo.boardCells[0] == 'X' && boardThree.boardCells[0] == 'X')
            {
                score++;
                Console.WriteLine("did it make it");
            }
            if (boardOne.boardCells[1] == 'X' && boardTwo.boardCells[1] == 'X' && boardThree.boardCells[1] == 'X')
                score++;
            if (boardOne.boardCells[2] == 'X' && boardTwo.boardCells[2] == 'X' && boardThree.boardCells[2] == 'X')
                score++;
            if (boardOne.boardCells[3] == 'X' && boardTwo.boardCells[3] == 'X' && boardThree.boardCells[3] == 'X')
                score++;
            if (boardOne.boardCells[4] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[4] == 'X')
                score++;
            if (boardOne.boardCells[5] == 'X' && boardTwo.boardCells[5] == 'X' && boardThree.boardCells[5] == 'X')
                score++;
            if (boardOne.boardCells[6] == 'X' && boardTwo.boardCells[6] == 'X' && boardThree.boardCells[6] == 'X')
                score++;
            if (boardOne.boardCells[7] == 'X' && boardTwo.boardCells[7] == 'X' && boardThree.boardCells[7] == 'X')
                score++;
            if (boardOne.boardCells[8] == 'X' && boardTwo.boardCells[8] == 'X' && boardThree.boardCells[8] == 'X')
                score++;
            if (boardOne.boardCells[0] == 'X' && boardTwo.boardCells[1] == 'X' && boardThree.boardCells[2] == 'X')
                score++;
            if (boardOne.boardCells[2] == 'X' && boardTwo.boardCells[1] == 'X' && boardThree.boardCells[0] == 'X')
                score++;
            if (boardOne.boardCells[3] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[5] == 'X')
                score++;
            if (boardOne.boardCells[5] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[3] == 'X')
                score++;
            if (boardOne.boardCells[6] == 'X' && boardTwo.boardCells[7] == 'X' && boardThree.boardCells[8] == 'X')
                score++;
            if (boardOne.boardCells[8] == 'X' && boardTwo.boardCells[7] == 'X' && boardThree.boardCells[6] == 'X')
                score++;
            if (boardOne.boardCells[0] == 'X' && boardTwo.boardCells[3] == 'X' && boardThree.boardCells[6] == 'X')
                score++;
            if (boardOne.boardCells[6] == 'X' && boardTwo.boardCells[3] == 'X' && boardThree.boardCells[0] == 'X')
                score++;
            if (boardOne.boardCells[1] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[7] == 'X')
                score++;
            if (boardOne.boardCells[7] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[1] == 'X')
                score++;
            if (boardOne.boardCells[2] == 'X' && boardTwo.boardCells[5] == 'X' && boardThree.boardCells[8] == 'X')
                score++;
            if (boardOne.boardCells[8] == 'X' && boardTwo.boardCells[5] == 'X' && boardThree.boardCells[2] == 'X')
                score++;
            if (boardOne.boardCells[0] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[8] == 'X')
                score++;
            if (boardOne.boardCells[8] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[0] == 'X')
                score++;
            if (boardOne.boardCells[2] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[6] == 'X')
                score++;
            if (boardOne.boardCells[6] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[2] == 'X')
                score++;

        }
        public void CPUThreeDWinCheck(ThreeD boardOne, ThreeD boardTwo, ThreeD boardThree, ref int score)           //new function to check CPU ThreeD wins
        {
            if (boardOne.boardCells[0] == 'O' && boardTwo.boardCells[0] == 'O' && boardThree.boardCells[0] == 'O')
                score++;
            if (boardOne.boardCells[1] == 'O' && boardTwo.boardCells[1] == 'O' && boardThree.boardCells[1] == 'O')
                score++;
            if (boardOne.boardCells[2] == 'O' && boardTwo.boardCells[2] == 'O' && boardThree.boardCells[2] == 'O')
                score++;
            if (boardOne.boardCells[3] == 'O' && boardTwo.boardCells[3] == 'O' && boardThree.boardCells[3] == 'O')
                score++;
            if (boardOne.boardCells[4] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[4] == 'O')
                score++;
            if (boardOne.boardCells[5] == 'O' && boardTwo.boardCells[5] == 'O' && boardThree.boardCells[5] == 'O')
                score++;
            if (boardOne.boardCells[6] == 'O' && boardTwo.boardCells[6] == 'O' && boardThree.boardCells[6] == 'O')
                score++;
            if (boardOne.boardCells[7] == 'O' && boardTwo.boardCells[7] == 'O' && boardThree.boardCells[7] == 'O')
                score++;
            if (boardOne.boardCells[8] == 'O' && boardTwo.boardCells[8] == 'O' && boardThree.boardCells[8] == 'O')
                score++;
            if (boardOne.boardCells[0] == 'O' && boardTwo.boardCells[1] == 'O' && boardThree.boardCells[2] == 'O')
                score++;
            if (boardOne.boardCells[2] == 'O' && boardTwo.boardCells[1] == 'O' && boardThree.boardCells[0] == 'O')
                score++;
            if (boardOne.boardCells[3] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[5] == 'O')
                score++;
            if (boardOne.boardCells[5] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[3] == 'O')
                score++;
            if (boardOne.boardCells[6] == 'O' && boardTwo.boardCells[7] == 'O' && boardThree.boardCells[8] == 'O')
                score++;
            if (boardOne.boardCells[8] == 'O' && boardTwo.boardCells[7] == 'O' && boardThree.boardCells[6] == 'O')
                score++;
            if (boardOne.boardCells[0] == 'O' && boardTwo.boardCells[3] == 'O' && boardThree.boardCells[6] == 'O')
                score++;
            if (boardOne.boardCells[6] == 'O' && boardTwo.boardCells[3] == 'O' && boardThree.boardCells[0] == 'O')
                score++;
            if (boardOne.boardCells[1] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[7] == 'O')
                score++;
            if (boardOne.boardCells[7] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[1] == 'O')
                score++;
            if (boardOne.boardCells[2] == 'O' && boardTwo.boardCells[5] == 'O' && boardThree.boardCells[8] == 'O')
                score++;
            if (boardOne.boardCells[8] == 'O' && boardTwo.boardCells[5] == 'O' && boardThree.boardCells[2] == 'O')
                score++;
            if (boardOne.boardCells[0] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[8] == 'O')
                score++;
            if (boardOne.boardCells[8] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[0] == 'O')
                score++;
            if (boardOne.boardCells[2] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[6] == 'O')
                score++;
            if (boardOne.boardCells[6] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[2] == 'O')
                score++;

        }

        ~ThreeD()
        {
            for (int c = 0; c < 10; c++)
            {
                boardCells[c] = '\0';
            }


        }

    }

    static void Main(string[] args)
    {
        ThreeD boardOne;                                                                        //1st of three objects for first board
        ThreeD boardTwo;                                                                        //2nd
        ThreeD boardThree;                                                                      //3rd
        int boardSelection = 0;                                                                     //picks witch object to mark in 
        int counter = 0;                                                                        //counter for while loop
        int playerOneScore = 0;                                                                 //score for user
        int CPUScore = 0;                                                                       //score for cpu
        int CPUBoardSelection = 0;
        Random rand = new Random();
        int randomNum = rand.Next(1, 2);

        if (randomNum == 1)                                                                     // if randomNum = 1, user goes first
        {
            Console.WriteLine("You first");
            while (true)
            {
                boardOne.getBoardCells();                                               
                boardTwo.getBoardCells();
                boardThree.getBoardCells();

                Console.WriteLine("Choose which board you wish to mark in. (1 - 3)");       //promts you to pick board
                bool board = false;

Yes, the compiler is right : you're trying to use unassigned variables :是的,编译器是对的:您正在尝试使用未分配的变量

static void Main(string[] args)
{
    // It's just a declaration; no value assigned to boardOne; boardOne contains trash
    ThreeD boardOne;                                                                        
    // It's just a declaration; no value assigned to boardTwo; boardTwo contains trash 
    ThreeD boardTwo;                                                                  
    // It's just a declaration; no value assigned to boardThree; boardThree contains trash
    ThreeD boardThree;                                                                      
    ....   
    if (randomNum == 1)                                                                     
    {
        Console.WriteLine("You first");
        while (true)
        {
            boardOne.getBoardCells(); // <- And here you're trying to access the trash   

It should be something like that:它应该是这样的:

static void Main(string[] args)
{
    // boardOne is declared and assigned
    ThreeD boardOne = new ThreeD();                                                                         
    // boardTwo is declared and assigned
    ThreeD boardTwo = new ThreeD();                                                                  
    // boardThree is declared and assigned
    ThreeD boardThree = new ThreeD();                                                                      
    ....   
    if (randomNum == 1)                                                                     
    {
        Console.WriteLine("You first");
        while (true)
        {
            boardOne.getBoardCells(); // <- Quite OK   

Firstly, boardOne , boardTwo and boardThree are variables, in this case are local variables scoped to the Main() method.首先, boardOneboardTwoboardThree是变量,在本例中是作用域为Main()方法的局部变量 Like any other variable, they need a valid data type, in your case the type is the ThreeD class.像任何其他变量一样,它们需要有效的数据类型,在您的情况下,类型是ThreeD类。 But this alone don't makes them objects, only defines their data type.但这本身并不能使它们成为对象,而只是定义了它们的数据类型。

The variables only become objects when you use this class to create a new instance (a new single object in memory).只有当您使用此类创建新实例(内存中的新单个对象)时,变量才会成为对象。 So they must be initialized :所以它们必须被初始化

ThreeD boardOne = new ThreeD();
ThreeD boardTwo = new ThreeD();
ThreeD boardThree = new ThreeD();

This way, when the method getBoardCells() is called, each variable points to the object in memory they represent, which contains that method.这样,当调用getBoardCells()方法时,每个变量都指向它们代表的内存中的对象,该对象包含该方法。 Without the assignment, the variables are equal null by default.如果没有赋值,默认情况下变量等于null And of course, as null haven't a method getBoardCells() the compiler error you got makes all sense.当然,因为null没有getBoardCells()方法, getBoardCells()你得到的编译器错误是有道理的。

In order to use a local variable, you have to initialize it:为了使用局部变量,你必须初始化它:

var boardOne = new ThreeD();
var boardTwo = new ThreeD();
...

You need to instantiate the class:您需要实例化该类:

ThreeD boardOne = new ThreeD();
ThreeD boardTwo = new ThreeD();
ThreeD boardThree = new ThreeD();

If you don't do this you cannot access the non-static class members.如果不这样做,则无法访问非静态类成员。

You have to initialize your board objects becuase C# compiler does not allow the use of uninitialized variables.您必须初始化您的板对象,因为 C# 编译器不允许使用未初始化的变量。 Check this link : Compiler Error CS0165检查此链接:编译器错误 CS0165

static void Main(string[] args)
{
    ThreeD boardOne = new ThreeD();                                                                        //1st of three objects for first board
    ThreeD boardTwo = new ThreeD();                                                                        //2nd
    ThreeD boardThree = new ThreeD();    

   //..............
   // ..............
 }

your missing creating object instance您丢失的创建对象实例

ThreeD boardOne = new ThreeD(); ThreeD boardOne = new ThreeD();

ThreeD boardTwo = new ThreeD(); ThreeD boardTwo = new ThreeD();

ThreeD boardThree = new ThreeD(); ThreeD boardThree = new ThreeD();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM