class Car {
let color: String
let model: String
var doors: [Int:String] = [:]
let brand: String
let state: String
init (color: String, model: String, doors: [Int:String], brand: String, state: String) {
self.color = color
self.model = model
self.doors = doors
self.brand = brand
self.state = state
}
func openDoor (numberDoor: Int) {
}
func closeDoor (numberDoor: Int) {
}
}
let car = Car(color: "Black", model: "A8", doors: [1:"Door #1", 2: "Door #2"], brand: "Audi", state: "Stop")
我输入门的号码,它在控制台上显示一条消息;门是打开的";,如果我用相同的号码再次输入;门已经打开";。门牌号码记录在字典里。
示例car.openDoor(编号:2(//";门2被打开";car.openDoor(编号:2(//";门2已打开
var alreadyOpen: [Int] = []
func openDoor (numberDoor: Int) {
if alreadyOpen.contains(numberDoor) {
print("door is already opend")
} else {
print("door is open")
alreadyOpen.append(numberDoor)
}
}
创建一个名为"的var;alreadyOpen";
var alreadyOpen: [Int] = []
如果这个数字在[arleadyOpen]数组中可用,则打印("门已经打开"(否则将打印("门打开"(并在数组中添加一个元素。
if alreadyOpen.contains(numberDoor)