发生条件后文本框停止更新

时间:2018-10-13 19:44:35

标签: c# textbox

在此程序中,无论何时发生其他情况下的任何语句,它都不会返回状态0并在该状态下执行操作,这就是我要尝试做的。而是,txtStatus和txtScore框仅继续显示“再次滚动!”跳转到状态2时显示的是什么。

   int die1 = 0;
    int die2 = 0;
    int total = 0;
    int state = 0;
    int point = 0;
    //int point2;
    int score = 0;

    //private int score;
    //private int state;
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {

    }

    private void btnRoll_Click(object sender, EventArgs e)
    {



        picDie1L.Visible = false;
        picDie1R.Visible = false;
        picDie2L.Visible = false;
        picDie2R.Visible = false;
        picDie3L.Visible = false;
        picDie3R.Visible = false;
        picDie4L.Visible = false;
        picDie4R.Visible = false;
        picDie5L.Visible = false;
        picDie5R.Visible = false;
        picDie6L.Visible = false;
        picDie6R.Visible = false;


        Random rand = new Random();
        die1 = rand.Next(1, 7);
        die2 = rand.Next(1, 7);
        total = (die1 + die2);

        txtDie1.Text = die1.ToString();
        txtDie2.Text = die2.ToString();
        txtTotal.Text = total.ToString();

        {
            if (die1 == 1)
            {
                picDie1L.Visible = true;
            }
            if (die1 == 2)
            {
                picDie2L.Visible = true;
            }
            if (die1 == 3)
            {
                picDie3L.Visible = true;
            }
            if (die1 == 4)
            {
                picDie4L.Visible = true;
            }
            if (die1 == 5)
            {
                picDie5L.Visible = true;
            }
            if (die1 == 6)
            {
                picDie6L.Visible = true;
            }
            if (die2 == 1)
            {
                picDie1R.Visible = true;
            }
            if (die2 == 2)
            {
                picDie2R.Visible = true;
            }
            if (die2 == 3)
            {
                picDie3R.Visible = true;
            }
            if (die2 == 4)
            {
                picDie4R.Visible = true;
            }
            if (die2 == 5)
            {
                picDie5R.Visible = true;
            }
            if (die2 == 6)
            {
                picDie6R.Visible = true;
            }               
        if (state == 0)
            {
                picPuck4.Visible = false;
                picPuck5.Visible = false;
                picPuck6.Visible = false;
                picPuck8.Visible = false;
                picPuck9.Visible = false;
                picPuck10.Visible = false;
                txtStatus.Text = string.Empty;
                state = 1;
                txtPoint.Text = string.Empty;

            }
        }
        if (state == 1)


        {
            if (total == 7 || total == 11)
            {
                txtStatus.Text = "You are a winner!";
                score++;
                txtScore.Text = Convert.ToString(score);
                state = 0;

            }
            if (total == 2 || total == 3 || total == 12)
            {
                txtStatus.Text = "You lose. Play again!";
                score--;
                txtScore.Text = Convert.ToString(score);
                state = 0;

            }
            if (total == 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10 || total == 12)
            {


                txtStatus.Text = "Roll again!";
                point = int.Parse(txtTotal.Text);
                txtPoint.Text = point.ToString();
                state = 2;

                if (total == 4)
                    picPuck4.Visible = true;
                if (total == 5)
                    picPuck5.Visible = true;
                if (total == 6)
                    picPuck6.Visible = true;
                if (total == 8)
                    picPuck8.Visible = true;
                if (total == 9)
                    picPuck9.Visible = true;
                if (total == 10)
                    picPuck10.Visible = true;



            }
        }
        else        
        {
            if (point == total)
            {
                txtStatus.Text = "You are a winner!";
                score++;
                txtScore.Text = Convert.ToString(score);
                state = 0;
            }
            if (total == 7)
            {
                txtStatus.Text = "You lose. Play again!";
                score--;
                txtScore.Text = Convert.ToString(score);
                state = 0;
            }
            if (total != 7 || point != total)
            {
                txtStatus.Text = "Roll again!";
                state = 2;
            }
        }




    }
}

}

2 个答案:

答案 0 :(得分:0)

也许问题出在这里

if (total != 7 || point != total)
{
    txtStatus.Text = "Roll again!";
    state = 2;
}

如您所见,大多数情况下情况都是如此!唯一错误的时间是 total == point == 7

答案 1 :(得分:0)

我认为您应该将最后一个parent语句更改为此:

else

说明:如果if (point == total) { txtStatus.Text = "You are a winner!"; score++; txtScore.Text = Convert.ToString(score); state = 0; } else if (total == 7) { txtStatus.Text = "You lose. Play again!"; score--; txtScore.Text = Convert.ToString(score); state = 0; } else { txtStatus.Text = "Roll again!"; state = 2; } 的值除7外,您都将 state 切换为2。因此,再次滚动时,您会立即转到最后一个{ {1}}语句并执行其中的3条total语句。在这种情况下,最后一个else语句始终为true(因为在这种情况下if永远不会为7),因此文本保持“再次滚动”并且状态永远不会更改(保持2)。

这将解决您的问题,但是我认为您需要更改逻辑(例如,if始终等于total,因为point可能不是您期望的行为)

希望这会有所帮助。