2015-10-10 16 views
-1

我基本上必須編碼一個紅綠燈,然後紅到綠再回到紅色。我設法讓交通信號燈從紅色閃爍變爲綠色,但反之亦然,有人看我的代碼,看看我在if和else語句上做了什麼錯誤。If Else StatementsTraffic Light Coding Java Blue J

/** * Write a description of class TrafficLight here. * * 
    @author (your name) * @version (a version number or a date) */ 
    public class TrafficLight { 
     // instance variables - replace the example below with your own 
     private Circle red; 
     private Circle yellow; 
     private Circle green; 
     private Boolean stop; 

     /** 
     * Constructor for objects of class TrafficLight 
     */ 
     public TrafficLight() 
     { 

      red = new Circle(); 
      red.changeColor("red"); 
      red.moveHorizontal(200); 
      red.moveVertical(200); 
      red.changeSize(60); 
      red.makeVisible(); 
      stop=true; 
      stop=false; 

      yellow = new Circle(); 
      yellow.changeColor("black"); 
      yellow.moveHorizontal(200); 
      yellow.moveVertical(260); 
      yellow.changeSize(60); 
      yellow.makeVisible(); 

      green = new Circle(); 
      green.changeColor("black"); 
      green.moveHorizontal(200); 
      green.moveVertical(320); 
      green.changeSize(60); 
      green.makeVisible(); 


      // Construct the circles 
      // Set their color and make them visible 
      // Set stop to be true; 
     } 

     /** 
     * If the traffic light shows STOP, it changes to GO. 
     * If the traffic light shows GO, it changes to STOP. 
     */ 
     public void change() { 

      // read the instructions for the user story 
      // of how this method should work 

      if (stop==false) { 
       red.changeColor("red"); 
       yellow.changeColor("yellow"); 
       green.changeColor("black"); 
       pause(); 
       red.changeColor("black"); 
       yellow.changeColor("black"); 
       green.changeColor("green"); 
       pause(); 

      } else{ 
       red.changeColor("black"); 
       yellow.changeColor("yellow"); 
       green.changeColor("black"); 
       pause(); 
       red.changeColor("red"); 
       yellow.changeColor("black"); 
       green.changeColor("black"); 
      } 

      /* 
      * Do not change anything in the pause method 
      */ 

     } 

     private void pause() { 
      try { 
       Thread.sleep(2000); 
      } catch (InterruptedException e) { 
      } 
     } 

    } 
+0

停止值始終設置爲false。這是問題。我可以看到你將stop初始化爲true並且再次爲false。嘗試改變它並以適當的方式初始化它。 –

+0

@ShiladittyaChakraborty你是什麼意思?我應該如何改變它? –

+0

if(stop == false)然後設置stop = true,反之亦然 –

回答

0
/** * Write a description of class TrafficLight here. * * 
@author (your name) * @version (a version number or a date) */ 
public class TrafficLight { 
    // instance variables - replace the example below with your own 
    private Circle red; 
    private Circle yellow; 
    private Circle green; 
    private Boolean stop = false; 

    /** 
    * Constructor for objects of class TrafficLight 
    */ 
    public TrafficLight() 
    { 

     red = new Circle(); 
     red.changeColor("red"); 
     red.moveHorizontal(200); 
     red.moveVertical(200); 
     red.changeSize(60); 
     red.makeVisible(); 

     yellow = new Circle(); 
     yellow.changeColor("black"); 
     yellow.moveHorizontal(200); 
     yellow.moveVertical(260); 
     yellow.changeSize(60); 
     yellow.makeVisible(); 

     green = new Circle(); 
     green.changeColor("black"); 
     green.moveHorizontal(200); 
     green.moveVertical(320); 
     green.changeSize(60); 
     green.makeVisible(); 


     // Construct the circles 
     // Set their color and make them visible 
     // Set stop to be true; 
    } 

    /** 
    * If the traffic light shows STOP, it changes to GO. 
    * If the traffic light shows GO, it changes to STOP. 
    */ 
    public void change() { 

     // read the instructions for the user story 
     // of how this method should work 

     if (stop==false) { 
      red.changeColor("red"); 
      yellow.changeColor("yellow"); 
      green.changeColor("black"); 
      pause(); 
      red.changeColor("black"); 
      yellow.changeColor("black"); 
      green.changeColor("green"); 
      pause(); 
      stop = true; 

     } else{ 
      red.changeColor("black"); 
      yellow.changeColor("yellow"); 
      green.changeColor("black"); 
      pause(); 
      red.changeColor("red"); 
      yellow.changeColor("black"); 
      green.changeColor("black"); 
      stop = false; 
     } 

     /* 
     * Do not change anything in the pause method 
     */ 

    } 

    private void pause() { 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
     } 
    } 

} 
+0

它仍然不起作用 –

+0

從哪裏調用方法? –

+0

else語句仍然不運行 –