簡體   English   中英

switch語句:迅速從if語句轉換為switch語句

[英]switch statement: converting from if-statement to switch statement in swift

我正在嘗試從下面的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 meal {
let meal = "breakfast"

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

   switch meal {

   case "breakfast":

         print("Good morning")

   case "lunch":

          print("Good afternoon!")

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

          print("default case")
   }

將此開關塊放在您的代碼中:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM