Qua*_*nel 0 c# counter boolean
我是一名绝对的初学程序员,正在使用visual studio 2012学习C#.
我创建了一个涉及计数器的小型表单程序.我想要为每个刻度改变颜色的背景,但我无法理解为什么我的代码不起作用.颜色改变一次然后保持这种状态.我必须犯一个根本性的错误.
任何人都可以看到这个问题是什么?
public Form1()
{
InitializeComponent();
timer1.Start();
}
int timeLeft = 60;
bool metronome = true;
public void timer1_Tick(object sender, EventArgs e)
{
timer1.Interval = 1000;
if (timeLeft > 0)
{
timeLeft = timeLeft - 1;
timeLabel.Text = Convert.ToString(timeLeft);
Metronome();
}
else
{
// Stop the timer
timer1.Stop();
}
}
public void Metronome()
{
metronome = !metronome;
if (metronome = true)
{
this.BackColor = System.Drawing.Color.Crimson;
}
else
{
this.BackColor = System.Drawing.Color.Black;
}
}
Run Code Online (Sandbox Code Playgroud)
你在这个条件上犯了一个错误:
if (metronome = true)
Run Code Online (Sandbox Code Playgroud)
您在这里使用赋值运算符.那么,你只需做的是设置true到metronome,然后的价值metronome始终true.它应该是相等运算符:
if (metronome == true)
Run Code Online (Sandbox Code Playgroud)
或者更好:
if (metronome)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
170 次 |
| 最近记录: |