在控制流中使用 switch 语句的问题。 Swift 3
Problems using switch statements in control flow. Swift 3
在这里,我试图将不同的首都附加到他们的大陆地区。有人可以告诉我我做错了什么吗?
var europeanCapitals: [String] = []
var asianCapitals: [String] = []
var otherCapitals: [String] = []
let world = [
"BEL": "Brussels",
"LIE": "Vaduz",
"BGR": "Sofia",
"USA": "Washington D.C.",
"MEX": "Mexico City",
"BRA": "Brasilia",
"IND": "New Delhi",
"VNM": "Hanoi"]
for (key, value) in world {
// Enter your code below
switch world {
case "BEL", "LIE", "BGR" : var europeanCapitals.append(value);
case "VNM", "IND" : var asianCapitals.append(value);
default: var otherCapitals.append(value);
}
// End code
}
您需要匹配 key
,而不是 world
,并且您的案例中的 var
没有意义。
var europeanCapitals: [String] = []
var asianCapitals: [String] = []
var otherCapitals: [String] = []
let world = [
"BEL": "Brussels",
"LIE": "Vaduz",
"BGR": "Sofia",
"USA": "Washington D.C.",
"MEX": "Mexico City",
"BRA": "Brasilia",
"IND": "New Delhi",
"VNM": "Hanoi"]
for (key, value) in world {
// Enter your code below
switch key {
case "BEL", "LIE", "BGR" : europeanCapitals.append(value);
case "VNM", "IND" : asianCapitals.append(value);
default: otherCapitals.append(value);
}
// End code
}
在这里,我试图将不同的首都附加到他们的大陆地区。有人可以告诉我我做错了什么吗?
var europeanCapitals: [String] = []
var asianCapitals: [String] = []
var otherCapitals: [String] = []
let world = [
"BEL": "Brussels",
"LIE": "Vaduz",
"BGR": "Sofia",
"USA": "Washington D.C.",
"MEX": "Mexico City",
"BRA": "Brasilia",
"IND": "New Delhi",
"VNM": "Hanoi"]
for (key, value) in world {
// Enter your code below
switch world {
case "BEL", "LIE", "BGR" : var europeanCapitals.append(value);
case "VNM", "IND" : var asianCapitals.append(value);
default: var otherCapitals.append(value);
}
// End code
}
您需要匹配 key
,而不是 world
,并且您的案例中的 var
没有意义。
var europeanCapitals: [String] = []
var asianCapitals: [String] = []
var otherCapitals: [String] = []
let world = [
"BEL": "Brussels",
"LIE": "Vaduz",
"BGR": "Sofia",
"USA": "Washington D.C.",
"MEX": "Mexico City",
"BRA": "Brasilia",
"IND": "New Delhi",
"VNM": "Hanoi"]
for (key, value) in world {
// Enter your code below
switch key {
case "BEL", "LIE", "BGR" : europeanCapitals.append(value);
case "VNM", "IND" : asianCapitals.append(value);
default: otherCapitals.append(value);
}
// End code
}