更改swift中自动打印数字的某一串 4

Change a certain string of an automatically printed number in swift 4

我想在 IOS playground 中复制此页面 http://www.99-bottles-of-beer.net/lyrics.html 进行练习,但我想问一个问题,希望得到您的帮助。我创建了一个名为 vantanoveBottles 的函数,创建了一个字符串类型的变量 "texts" 并使用 "for and in" 来表示 0 ... 5 之间的数字 我创建了一个字符串恒常性,在其中输入数字和文本,然后添加文本+ newLine 如 code.Now 如果你比较网站时说没有更多的啤酒,不要使用数字 0,我会退出,所以我基本上会打印 1 到 98 的示例数字与文本字符串相反,(我这样做了)然后来到 1 should not go out "1 bottles of beer on the wall, 1 bottles of beer.\n" 取下并传递它,0 bottles of beer on墙。 》这个0一定要消不出去,我怎么办?这是测试代码

func novantanoveBottles () ->String {
    var testi: String = ""

    for numeri in (1...99).reversed() {
        let newLine:String = "\n \(numeri) bottles of beer on the wall, \(numeri) bottles of beer.\n Take one down and pass it around, \(numeri - 1) bottles of beer on the wall. \n"
        testi += newLine
    }

    testi += "\n \n \n No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall."

    return testi
}

print(novantanoveBottles())

您可以在添加该部分文本之前添加一个检查

for numeri in (1...99).reversed() {
    let newLine:String = "\n \(numeri) bottles of beer on the wall, \(numeri) bottles of beer.\n Take one down and pass it around"
    if numeri != 1 {
        test1 += ", \(numeri - 1) bottles of beer on the wall. \n
    } else {
        test1 += ".\n"
    }
    testi += newLine
}