首页 > 解决方案 > 在 Ios 中,我需要接受三角形三边的长度作为输入,输出应指示三角形的类型

问题描述

我已经弄清楚了逻辑,但不知道如何编写代码。我不确定如何在 ios 上使用 if else 语句

If a==b && b==c Eqilateral If a==b or b==c or c==a Issoceles Else Scalene

标签: iosswiftxcode

解决方案


您可以使用 Set 来确定三角形类型

func triangleType(lengthSet: Set<Int>) -> String {
        switch lengthSet.count {
        case 1:
            return "Equilateral"
        case 2:
            return "Isosceles"
        default:
            return "Scalene"
        }
    }

用法 : self.triangleType(lengthSet: [2,2,3])

建议在调用函数之前评估您的输入计数;)


推荐阅读