Define a traffic light enumeration class, including red, green, yellow lights, need to get the next light method, and to achieve a red light 5 seconds after the turn of the green light, green 3 seconds after the yellow lamp, yellow light after 2 seconds into a red light, so cycle
1 Public classTEST5 {2 3 /**4 * @throwsinterruptedexception5 */6 Public Static voidMain (string[] args)throwsinterruptedexception {7deng[] Deng =Deng. VALUES ();8 for(inti = 0;i < 3;i++){9Deng d =Deng[i];Ten if(d = =Deng. Red) { One System. out.println (d); AThread. Sleep (5000); - System. Out.println (D.next ()); -}Else if(d = =Deng. Green) { theThread. Sleep (3000); - System. Out.println (D.next ()); -}Else if(d = =Deng. Yellow) { -Thread. Sleep (2000); + //System.out.println (D.next ()); -i =-1; + } A } at } - } - enumdeng{ -Red ("Red light"){ - PublicDeng Next () { - returnGreen; in } -},green ("green light")){ to PublicDeng Next () { + returnYellow; - } the},yellow ("Yellow Light"){ * PublicDeng Next () { $ returnRed;Panax Notoginseng } - }; the Public AbstractDeng Next (); + PrivateString name; A PrivateDeng (String name) { the This. Name =name; + } - @Override $ PublicString toString () { $ returnname; - } -}
Java Learning Notes----enumeration test questions