請問各位大神
if()
else if ()
else if ()
該怎麼寫?
才能一個一個照順序閃?
請求各位大神教教小弟
程式碼區塊
private void Button1_Click(object sender, EventArgs e)
{
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
if ()
{
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.White);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.Red);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
}
else if ()
{
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.Blue);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.White);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
}
else if ()
{
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.White);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.Green);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
}
aTimer.Interval++;
}
程式碼區塊
private void Button1_Click(object sender, EventArgs e)
{
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
//用 %3 就可以得到 3 種情形,就是 0,1,2
double what;
//用 aTimer.interval 用這列,應該會閃得快到看不清楚
what = aTimer.interval;
//若只要每秒閃一次可以用這列
//what = e.SignalTime.Second;//取得當時「秒」(0..59)
if (what%3==0)
{
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.White);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.Red);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
}
else if (what%3==1)
{
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.Blue);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.White);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
}
else if (what%3==2)
{
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.White);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.Green);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
}
aTimer.Interval++;
}
選我正解
No, I never use else if in any case.
Switch (what%3){
Case 0:
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.White);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.Red);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
break;
Case:1
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.Blue);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.White);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
break;
Case:2
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.White);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.Green);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
break;
}
aTimer.Interval++;
if the swich case can't spilt you logic.
我只會使用if,使用contine, break and return.
"else if" is dirty code.
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
aTimer.Interval++;
if ()
{
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.White);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.Red);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
return;
}
if ()
{
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.Blue);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.White);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
return;
}
if ()
{
Graphics gra = this.pictureBox1.CreateGraphics();
Brush bush = new SolidBrush(Color.White);
gra.FillEllipse(bush, 10, 10, 200, 200);
Graphics gra1 = this.pictureBox2.CreateGraphics();
Brush bush1 = new SolidBrush(Color.White);
gra1.FillEllipse(bush1, 10, 10, 200, 200);
Graphics gra2 = this.pictureBox3.CreateGraphics();
Brush bush2 = new SolidBrush(Color.Green);
gra2.FillEllipse(bush2, 10, 10, 200, 200);
return;
}
}