You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不修改IF语句的情况下终止C#中的FOR循环?

解决GTA作弊码程序的循环问题

要解决你的问题,只需做两处关键修改:终止密码验证的for循环,同时添加支持重复选择作弊码的循环逻辑,且不需要修改原有的if判断语句:

修改后的完整代码

string password = "12345";
string userInput;
string userChoice;
int triesCount = 5;

for (int i = 0; i < triesCount; i++)
{
    Console.Write("Enter password: ");
    userInput = Console.ReadLine();
    if (userInput == password)
    {
        // 用while循环实现重复选择作弊码
        while (true)
        {
            Console.WriteLine("\nCode for GTA San Andreas - enter a number to view the list of codes:");
            Console.WriteLine("1 - Codes for weapons");
            Console.WriteLine("2 - Codes for cars");
            Console.WriteLine("3 - Cheats for health, armor and money");
            Console.WriteLine("0 - Exit"); // 添加退出选项
            Console.Write("Your choice: ");
            userChoice = Console.ReadLine();

            switch (userChoice)
            {
                case "1":
                    Console.WriteLine("----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                    Console.WriteLine("LXGIWYL - набор оружия №1 для любителей (кастет, бита, пистолет 9mm, дробовик, micro SMG, AK-47, винтовка, ракетница, коктейль Молотова, баллончик с краской).");
                    Console.WriteLine("KJKSZPJ - набор оружия №2 для профессионалов (нож, пистолет Desert Eagle, обрез (дробовик Sawn-off), Tec-9, M4, снайперская винтовка, огнетушитель, огнемет, гранаты).");
                    Console.WriteLine("UZUMYMW - набор оружия №3 для психов (бензопила, пистолет с глушителем, боевой дробовик (Combat shotgun), MP5, M4, Stinger, взрывчатка с дистанционным пультом).");
                    Console.WriteLine("----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                    break;
                case "2":
                    Console.WriteLine("-------------------------------");
                    Console.WriteLine("AIWPRTON - танк Rhino");
                    Console.WriteLine("PDNEJOH - Hotring Racer 73");
                    Console.WriteLine("AGBDLCID - Monster");
                    Console.WriteLine("-------------------------------");
                    break;
                case "3":
                    Console.WriteLine("------------------------------------------------------------");
                    Console.WriteLine("HESOYAM  - полное здоровье, броня и $250.000.");
                    Console.WriteLine("BAGUVIX - бесконечное здоровье с защитой от пуль, падения.");
                    Console.WriteLine("MUNASEF - уровень адреналина.");
                    Console.WriteLine("------------------------------------------------------------");
                    break;
                case "0":
                    Console.WriteLine("Exiting program...");
                    break; // 退出switch
                default:
                    Console.WriteLine("Invalid choice, please try again.");
                    continue; // 回到循环开头重新选择
            }

            if (userChoice == "0")
            {
                break; // 退出while循环
            }
        }

        break; // 终止外层的for循环,不再提示输入密码
    }
    else
    {
        Console.WriteLine("\nWrong password entered.");
        Console.WriteLine($"you have left {triesCount - (i + 1)} attempts.\n");
    }
}

Console.ReadKey();

关键修改说明

  1. 终止for循环:在密码验证成功的if块末尾(while循环结束后)添加break;语句,直接跳出外层的for循环,避免程序再次回到输入密码的步骤。
  2. 添加重复选择逻辑:将作弊码菜单、选择判断的代码包裹到while (true)循环中,同时新增0 - Exit选项,用户可以多次选择不同类别,输入0后退出程序。
  3. 不修改原IF语句:原有的密码验证if判断完全保留,仅在其内部添加循环和终止语句,符合你的要求。

内容的提问来源于stack exchange,提问作者AMIRRO7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 09:35:37