switch语句:迅速从if语句转换为switch语句

时间:2018-08-04 02:11:10

标签: swift switch-statement control-flow

我正在尝试从下面的if语句转换为switch语句,但是我一直在出错。

let meal = "breakfast"

if meal == "breakfast" {
    print("Good morning!")
} else if meal == "lunch" {
    print("Good afternoon!")
} else if meal == "dinner" {
    print("Good evening!")
} else {
    print("Hello!")
} 

这是我的switch声明:

switch meal {
let meal = "breakfast"

    case 1: 
        print("Good morning!”)
    case 2: 
        print("Good afternoon!")
    case 3: 
        print("Good evening!")
}

1 个答案:

答案 0 :(得分:1)

  let meal = "breakfast"

   switch meal {

   case "breakfast":

         print("Good morning")

   case "lunch":

          print("Good afternoon!")

   case "dinner":
          print("Good evening!")
   default:

          print("default case")
   }

将此开关块放入您的代码中:)