iT邦幫忙

0

C# IF 跟else if 裡的內容

c#

請問各位大神
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++;

        }
看更多先前的討論...收起先前的討論...
dragonH iT邦超人 5 級 ‧ 2019-10-04 09:15:30 檢舉
if else 是條件語句

符合條件才會執行裡面的內容

所以你執行的條件分別是?
sion iT邦新手 4 級 ‧ 2019-10-04 09:20:07 檢舉
他的需求應該要寫
if()
if()
if()
原本IF裡條件是
aTimer.Interval % 2 ==0
可是只能顯示2種
第3種無法顯示
dragonH iT邦超人 5 級 ‧ 2019-10-04 09:26:31 檢舉
阿 if else 就是只會執行其中一個呀

你要符合條件就執行

就要跟 sion 大 說的一樣

用 if
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2019-10-04 09:43:54
最佳解答
程式碼區塊
 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++;

        }

選我正解

真的神
小弟在此謝過
也感謝其他大神的教導

0
zain
iT邦新手 5 級 ‧ 2019-10-04 11:08:54

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;

            }

        }

我要發表回答

立即登入回答