gpt4 book ai didi

swift - 在 Swift 中的函数内调用枚举

转载 作者:行者123 更新时间:2023-11-30 12:34:00 27 4
gpt4 key购买 nike

我对这个完全不知所措。我的导师在寻找什么?有什么建议吗?

开始了类似的事情,但我刚刚开始..

enum Color: String {

case blue = "orange"
case red = "green"
case yellow = "purple"

}


func complement(_ color) -> String {
case
}

问题:使用三种原色和三种间色创建一个类型 Color。然后创建一个函数 complement ,它将返回给定颜色的补色。

Red's complement is green
Blue's complement is orange
Yellow's complement is purple

complement(.red) -> .green
complement(.orange) -> .blue

更新:

enum Color: String {
case red = "red"
case green = "green"
case blue = "blue"

case yellow = "yellow"
case orange = "orange"
case purple = "purple"
}

func compliment(_ color: Color) -> Color {
switch color {
case .red: return .green
case .green: return .red
case .blue: return .orange
break
}
}

let red = Color.red
let compliment = compliment(red)
print(compliment)

let green = Color.green
let compliment = compliment(green)
print(compliment)

let blue = Color.blue
let compliment = compliment(blue)
print(compliment)

error: variable used within its own initial value

let compliment = compliment(red)

它对每个实例都执行此操作

最佳答案

由于这是家庭作业,我会给你足够的指导来帮助你开始,但不会为你做。

enum Color: String {
case red = "red"
case green = "green"
case blue = "blue"

case yellow = "yellow"
case orange = "orange"
case purple = "purple"
}

func complement(_ color: Color) -> Color {
switch color {
case .red: return .green
case .green: return .red
case .blue: return .orange
// etc.
}
}

let red = Color.red
let compliment = complement(red)
print(compliment)

在不深入了解颜色理论的情况下,红色的补色不是蓝色 - 它是青色。对于绿色,它是洋红色。蓝色的补色是黄色。

关于swift - 在 Swift 中的函数内调用枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43108001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com