do/while循环声明等错误

Do / while loop declaration and other errors

我在尝试实现这个基本的 do while 循环时遇到问题。

我一直收到错误提示

"statements are not allowed at the top level"

或另一个错误(突出显示单词 "do")说

"expected declaration"

我查看了swift 编程指南,并多次阅读了相关章节。我也看过我能找到的每一个视频,但似乎无法弄清楚我到底做错了什么。

这是代码。

//  loop.swift

import Foundation

var i = 17

do
{
   println("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16")
} while (i < 16)

如果这不是您的 main.swift 文件(它似乎是您的 loop.swift 文件),您必须将代码包装在一个函数中。您可以从 main.swift 或其他地方的其他函数调用该函数。