gpt4 book ai didi

swift - case 语句中 Unresolved 标识符错误

转载 作者:行者123 更新时间:2023-11-30 13:06:53 27 4
gpt4 key购买 nike

此代码在 perimeter() 函数内导致“无法解析的标识符”错误:

enum ShapeDimensions {
case Square(Double) // Associated value defined by length of one side
case Rectangle(width: Double, height: Double) // Associate value defined by width and height
case Point // No Associated value
case RightTriangle(baseLength: Double, sideHeight: Double) // Associated value defined by base length and height

func area() -> Double {
switch self {
case let .Square(side):
return side * side
case let .Rectangle(width: w, height: h):
return w * h
case .Point:
return 0.0
case .RightTriangle(baseLength: l, sideHeight: h):
return 0.5 * l * h
}
}

func perimeter() -> Double {
switch self {
case let .Square(side):
return 4 * side
case let .Rectangle(width: w, height: h):
return 2 * w + 2 * h
case .Point:
return 0.0
case .RightTriangle(baseLength: l, sideHeight: h):
return sqrt(l * l + h * h) + l + h
}
}

}

var squareShape = ShapeDimensions.Square(10.0)
var rectShape = ShapeDimensions.Rectangle(width: 5.0, height: 10.0)
var pointShape = ShapeDimensions.Point
var rightTriangleShape = ShapeDimensions.RightTriangle(baseLength: 5.0, sideHeight: 10.0)

print("Square's area = \(squareShape.area())")
print("Retangle's area = \(rectShape.area())")
print("Point's area = \(pointShape.area())")
print("Right Triangle's area = \(rightTriangleShape.area())")

print("Square's perimeter = \(squareShape.perimeter())")
print("Retangle's perimeter = \(rectShape.perimeter())")
print("Point's perimeter = \(pointShape.perimeter())")
print("Right Triangle's perimeter = \(rightTriangleShape.perimeter())")

我收到这些错误:

14-Enumerations.playground:221:29: error: use of unresolved identifier 'h'          return sqrt(l * l + h * h) + l + h                              ^   14-Enumerations.playground:221:33: error: use of unresolved identifier 'h'          return sqrt(l * l + h * h) + l + h                                  ^   14-Enumerations.playground:221:38: error: use of unresolved identifier 'l'          return sqrt(l * l + h * h) + l + h                                       ^    14-Enumerations.playground:221:42: error: use of unresolved identifier 'h'          return sqrt(l * l + h * h) + l + h 

代码有什么问题?

最佳答案

只需在每个案例之前添加 let 就可以了。

enum ShapeDimensions {
case Square(Double) // Associated value defined by length of one side
case Rectangle(width: Double, height: Double) // Associate value defined by width and height
case Point // No Associated value
case RightTriangle(baseLength: Double, sideHeight: Double) // Associated value defined by base length and height

func area() -> Double {
switch self {
case let .Square(side):
return side * side
case let .Rectangle(width: w, height: h):
return w * h
case let .Point:
return 0.0
case let .RightTriangle(baseLength: l, sideHeight: h):
return 0.5 * l * h
}
}

func perimeter() -> Double {
switch self {
case let .Square(side):
return 4 * side
case let .Rectangle(width: w, height: h):
return 2 * w + 2 * h
case let.Point:
return 0.0
case let .RightTriangle(baseLength: l, sideHeight: h):
return sqrt(l * l + h * h) + l + h
}
}

}

var squareShape = ShapeDimensions.Square(10.0)
var rectShape = ShapeDimensions.Rectangle(width: 5.0, height: 10.0)
var pointShape = ShapeDimensions.Point
var rightTriangleShape = ShapeDimensions.RightTriangle(baseLength: 5.0, sideHeight: 10.0)

print("Square's area = \(squareShape.area())")
print("Retangle's area = \(rectShape.area())")
print("Point's area = \(pointShape.area())")
print("Right Triangle's area = \(rightTriangleShape.area())")

print("Square's perimeter = \(squareShape.perimeter())")
print("Retangle's perimeter = \(rectShape.perimeter())")
print("Point's perimeter = \(pointShape.perimeter())")
print("Right Triangle's perimeter = \(rightTriangleShape.perimeter())")

关于swift - case 语句中 Unresolved 标识符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39258844/

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