Skip to content

ZH TW CSharp Loops

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

循環、中斷、繼續和返回

手冊C# · 範例UO

for指定初始動作、條件和變化; foreach 迭代序列; while 在迭代前檢查條件; do - 之後。 break 結束循環,continue 繼續下一個迭代,return 退出方法或腳本。

簡單範例 (Example.cs)

請將此範例另存為 Example.cs,並與本頁其他範例分開執行。

Example.cs:

// PROJECT-EXAMPLE
for (int i = 0; i < 3; i++)
    System.Console.WriteLine(i);

結果:

0
1
2
for (int attempt = 1; attempt <= 3; attempt++)
{
    UO.Print($"Attempt {attempt}");
    await WaitAsync(100);
}
string[] names = ["self", "backpack", "lasttarget"];
foreach (string name in names)
{
    if (name == "backpack") continue;
    UO.Print(UO.GetSerial(name));
}
int left = 3;
while (left > 0)
{
    left--;
    await WaitAsync(50);
}
do
{
    UO.Print("Runs at least once");
} while (false);

無限後台腳本在等待外部狀態時應該有明確的停止和等待條件。語言中存在goto,但常規循環和方法通常更清晰。

官方文件:循環、中斷、繼續和返回 - Microsoft Learn。

Clone this wiki locally