第一次按下,两个小灯全亮。2.第二次按下,蓝灯亮,黄灯灭。3.第三次按下,黄灯亮,蓝灯灭。4.第四次按下,两个小灯全灭代码
时间: 2024-10-16 22:05:30 浏览: 38
这似乎是在描述一个简单的状态机或者是按钮控制电路的行为。我们可以用Python编写一段伪代码来表示这个过程:
```python
class LightController:
def __init__(self):
self.blue_light = True
self.yellow_light = False
def button_press(self, press_number):
if press_number == 1:
self.blue_light = True
self.yellow_light = True
elif press_number == 2:
self.blue_light = not self.blue_light
self.yellow_light = not self.yellow_light
elif press_number == 3:
self.blue_light = self.yellow_light
self.yellow_light = not self.blue_light
elif press_number == 4:
self.blue_light = False
self.yellow_light = False
# 初始化控制器
controller = LightController()
# 模拟按钮按下
for press in [1, 2, 3, 4]:
controller.button_press(press)
print(f"Press {press}: Blue light is {'on' if controller.blue_light else 'off'}, Yellow light is {'on' if controller.yellow_light else 'off'}")
```
在这个例子中,`LightController`类有两个私有变量代表灯的状态,并通过`button_press`方法来更新它们。每次按下按钮,都会改变灯的状态。
阅读全文