文本框在条件发生后停止更新
Text box stops updating after a condition occurs
在这个程序中,每当 else 下的任何语句发生时,它都不会返回状态 0 并执行该状态下的操作,这正是我正在尝试做的。相反,txtStatus 和 txtScore 框继续显示 "Roll again!",这是跳转到状态 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;
}
}
}
}
}
可能问题出在这里
if (total != 7 || point != total)
{
txtStatus.Text = "Roll again!";
state = 2;
}
如您所见,条件在大多数情况下都是正确的!这是错误的唯一一次是 total == point == 7
我认为你应该将最后一个 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;
}
解释:当total
有除7以外的任何值时,你将state切换为2。正因为如此,当你再次滚动,您会立即转到最后一个 else
语句并执行其中的 3 个 if
语句。在这种情况下,最后的 if
语句始终为真(因为在这种情况下 total
永远不会是 7)因此文本保持 "Roll again" 并且状态永远不会改变(保持 2)。
这将解决您的问题,但我认为您需要更改逻辑(例如,point
始终等于 total
,因为可能不需要 point == txtTotal.text == total
行为)。
希望对您有所帮助。
在这个程序中,每当 else 下的任何语句发生时,它都不会返回状态 0 并执行该状态下的操作,这正是我正在尝试做的。相反,txtStatus 和 txtScore 框继续显示 "Roll again!",这是跳转到状态 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;
}
}
}
}
}
可能问题出在这里
if (total != 7 || point != total)
{
txtStatus.Text = "Roll again!";
state = 2;
}
如您所见,条件在大多数情况下都是正确的!这是错误的唯一一次是 total == point == 7
我认为你应该将最后一个 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;
}
解释:当total
有除7以外的任何值时,你将state切换为2。正因为如此,当你再次滚动,您会立即转到最后一个 else
语句并执行其中的 3 个 if
语句。在这种情况下,最后的 if
语句始终为真(因为在这种情况下 total
永远不会是 7)因此文本保持 "Roll again" 并且状态永远不会改变(保持 2)。
这将解决您的问题,但我认为您需要更改逻辑(例如,point
始终等于 total
,因为可能不需要 point == txtTotal.text == total
行为)。
希望对您有所帮助。