作者: ghostla (烤死人...) 站內: Talk
標題: Re: 一個java的問題什麼是 else without if
時間: Thu Jan 10 23:31:03 2008

※ 引述《sagat (SAGAT)》之銘言:
刪光光...

將你的paint這個方法改成以下

    public void paint(Graphics g) {
        int w = getSize().width;
        int h = getSize().height;
        g.setColor(Color.white);
        g.fillRect(0, 0, w - 1, h - 1);
        n++;
        System.out.println("~~~~n % 4 = " + n%4);
        if (n % 4 == 1) {
            g.setColor(Color.green);
            g.fillOval(w / 2 - d / 2, h / 2 - d / 2, d, d);
            timer.stop();
            timer = new Timer(5000, this);
            timer.start();
        } else if (n % 4 == 2) {

            g.setColor(Color.yellow);
            g.fillOval(w / 2 - d / 2, h / 2 - d / 2, d, d);
            timer.stop();
            timer = new Timer(5000, this);
            timer.start();
        } else if (n % 4 == 3) {

            g.setColor(Color.red);
            g.fillOval(w / 2 - d / 2, h / 2 - d / 2, d, d);
            timer.stop();
            timer = new Timer(5000, this);
            timer.start();

        } else {

            g.setColor(Color.yellow);
            g.fillOval(w / 2 - d / 2, h / 2 - d / 2, d, d);
            timer.stop();
            timer = new Timer(2000, this);
            timer.start();

        }
    }

因為你的n % 2只會有兩種可能,不是0就是1
所以你不能用那種判斷去寫if else
要用有4種判斷去寫才會對
你試試看吧



--
  Origin:  玩車人的天地 ˙東吳機研站  scumotor.com.tw 
  Author: ghostla 59-121-24-143.dynamic.hinet.net 發表keldon :不虧是鬼哥~                                            08/01/10
→ burgess999 :高手XD                                              08/01/11