【问题标题】:Error says im not using the things i type c#错误说我没有使用我输入的东西 c#
【发布时间】:2013-03-13 04:07:03
【问题描述】:

嗨,我正在尝试制作井字游戏。互联网上有很多代码和灵感,但我需要以某种方式制作它,而且似乎没有多少人使用这种方式,而且我遇到了麻烦。我需要使用数组,然后说我的 x = 10 和我的 y = 1,每当我得到 30 或 3 的总和时,我就会知道其中哪个赢了。

  1. 但是在winnercheck 的方法下,它说有一些错误,它说需要一个get 或set 访问器,我不知道那是什么??

  2. 除了它一直警告我,我已将 x 定义为 1,将 o 定义为 10,并且我的 player2 永远不会使用命令

  3. 我也不知道如何才能像我所说的那样对数字求和?

这是我的代码

namespace tictactoe
{
    public partial class Form1 : Form
    { 
         bool player1 = true;
         bool player2 = false; 
         int x=1;
         int o=10;

        public Form1()
        {
            InitializeComponent();
            value();
        }

        private string[] status;

        private void value()


        { 



            int[] status = new int[9];
            myarrays();

        }


        private void myarrays()

        {  
            button1.Text = status[0];
            button2.Text = status[1];
            button3.Text = status[2];
            button4.Text = status[3];
            button5.Text = status[4];
            button6.Text = status[5]; 
            button7.Text = status[6];
            button8.Text = status[7];
            button9.Text = status[8];

        }




        private void label1_Click(object sender, EventArgs e)
        {


        }
        private void Form1_Load(object sender, EventArgs e)
        { 
        }





        private void button1_Click(object sender, EventArgs e)
        {
            if (player1 == true)
            {
                player1 = true;
                button1.Text = "X";
                player1 = false;
            }
            else
            {
                player2 = true;
                button1.Text = "O";
                player2 = false;
                player1 = true;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (player1 == true)
            {
                player1 = true;
                button2.Text = "X";
                player1 = false;
            }
            else
            {
                player2 = true;
                button2.Text = "O";
                player2 = false;
                player1 = true;
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (player1 == true)
            {
                 player1 = true;
                button2.Text = "X";
                player1 = false;
            }
            else
            {
                player2 = true;
                button2.Text = "O";
                player2 = false;
                player1 = true;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (player1 == true)
            {
                 player1 = true;
                button2.Text = "X";
                player1 = false;
            }
            else
            {
                player2 = true;
                button2.Text = "O";
                player2 = false;
                player1 = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (player1 == true)
            {
                 player1 = true;
                button2.Text = "X";
                player1 = false;
            }
            else
            {
                player2 = true;
                button2.Text = "O";
                player2 = false;
                player1 = true;            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (player1 == true)
            {
                 player1 = true;
                button2.Text = "X";
                player1 = false;
            }
            else
            {
                player2 = true;
                button2.Text = "O";
                player2 = false;
                player1 = true;            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (player1 == true)
            {
                 player1 = true;
                button2.Text = "X";
                player1 = false;
            }
            else
            {
                player2 = true;
                button2.Text = "O";
                player2 = false;
                player1 = true;            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (player1 == true)
            {
                 player1 = true;
                button2.Text = "X";
                player1 = false;
            }
            else
            {
                player2 = true;
                button2.Text = "O";
                player2 = false;
                player1 = true;            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (førstespiller == true)
            {
                if (player1 == true)
            {
                 player1 = true;
                button2.Text = "X";
                player1 = false;
            }
            else
            {
                player2 = true;
                button2.Text = "O";
                player2 = false;
                player1 = true;
            }
        }


        private void checkwinner 
        {
        if (button1.Text =="X" & button2.Text =="X" & button3.Text= ="x")
            MessageBox.Show
            MessageBox.Show


        }


        private void button10Newgame_Click(object sender, EventArgs e)
        {

            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = "";
            player1 = true;
            player2 = false;
        }




        }

}

【问题讨论】:

  • 请仅列出相关代码

标签: c# boolean


【解决方案1】:
private void checkwinner

需要private void checkwinner()

【讨论】:

  • 并且还需要在 if 块周围有适当的 { }。
  • 而且实际上调用MessageBox.Show(...) 可能会有所帮助。
【解决方案2】:

您的代码存在许多问题,但我认为您在标题中引用的警告与此行有关:

int[] status = new int[9];

您正在创建一个新变量但从未实际使用它,value 中的 status 变量仅存在于该方法的范围内。而myarrays 中的status 变量引用成员字段string[] status

【讨论】:

  • 那我该怎么解决呢?我需要使用 int[] status = new int[9];因为我必须做一些事情来让程序将我的 x 读取为 10 并将 o 读取为 1,然后让它知道每当我得到 3 或 30 的总和时,这将是连续三个并且也是一个胜利的情况......你知道它应该是什么样子吗?它会这样认为,如果我的 button1+buttun2+button 3 =30 或 x 获胜并且 =3 O 获胜
  • 很抱歉,但很难准确地说出您想要做什么。从外观上看,您根本不需要 string[] status 字段,因为您直接将游戏状态存储在文本框中。
【解决方案3】:

仅关于您的第一个问题 -

MessageBox.Show

应该是 -

MessageBox.Show("you won!");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2014-02-07
    • 1970-01-01
    相关资源
    最近更新 更多