簡體   English   中英

在 Swift 中的 `switch` 語句中使用 `case` 和范圍

[英]Using `case` with range in `switch` statement in Swift

我是 swift 的新手,並在 Swift Playgrounds 中進行了一些測試。 嘗試使用switch ,但它不起作用。 有人能幫我嗎?

func loveCal() {
    let loveScore = Int.random(in: 0...100)

    switch loveScore {
    case 80...100:
        print("You love eachother like Kanye loves Kanye")
    case 40...80:
        print("You go together like coke and mentos")
    case 0...40:
        print("You'll be forever alone")
    }
}

loveCal()

您所缺少的只是default情況:

// ...
case 0...40:
    print("You'll be forever alone")
default:
    print("This should never happen")

暫無
暫無

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

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