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

C#登录完成后Main方法后续代码未执行问题求助

登录成功后Main方法后续代码不执行的问题解决

问题原因

  1. 递归调用导致流程无法正常返回:你的CheckAccount和AccessDenied方法使用递归调用,登录成功后程序需要逐层退出递归栈,加上LoginForm末尾冗余的Console.ReadLine(),即使isLoggedIn设为true,也会阻塞在输入等待环节,无法回到Main方法的循环判断。
  2. 冗余的输入等待:LoginForm最后一行的Console.ReadLine()完全多余,它会在登录流程完成后强制要求额外输入,直接阻断了后续代码的执行。

修改后的代码

using System;
using System.Collections.Generic;

namespace Recipe_Man
{
    internal class Program
    {
        private static Dictionary<string, string> keys = new Dictionary<string, string>();
        static int tries = 3;
        static bool isLoggedIn = false;
        
        static void Main(string[] args)
        {
            while (!isLoggedIn)
            {
                LoginForm();
            }
            CreateRecipe();
        }

        static void CreateRecipe()
        {
            Console.WriteLine("Finished");
        }

        static void LoginForm()
        {
            Console.Write("Login or Create: ");
            string loginInput = Console.ReadLine();
            string login = loginInput.ToLower();
            
            if (login == "create")
            {
                Console.Write("Username: ");
                string x = Console.ReadLine();
                Console.Write("Password: ");
                string y = Console.ReadLine();

                keys.Add(x, y);

                Console.WriteLine("Please login with your new account");
                ValidateAccount();
            }
            else if (login == "login")
            {
                ValidateAccount();
            }
        }

        // 用循环替代递归实现账号验证,避免栈溢出和流程阻塞
        static void ValidateAccount()
        {
            tries = 3; // 每次验证前重置尝试次数
            while (tries > 0 && !isLoggedIn)
            {
                Console.Write("Username: ");
                string username = Console.ReadLine();
                Console.Write("Password: ");
                string password = Console.ReadLine();

                if (keys.TryGetValue(username, out string storedPassword))
                {
                    if (storedPassword == password)
                    {
                        AccessGranted(username);
                        break;
                    }
                    else
                    {
                        tries--;
                        AccessDenied();
                    }
                }
                else
                {
                    Console.WriteLine("Username or Password isn't found");
                    tries--;
                    if (tries > 0)
                    {
                        Console.WriteLine($"You have {tries} more tries");
                    }
                }
            }
            
            if (tries == 0)
            {
                Banned();
            }
        }

        static void AccessGranted(string username)
        {
            isLoggedIn = true;
            Console.WriteLine($"Welcome {username}, we are here to make a fantastic experience for you");
        }

        static void AccessDenied()
        {
            Console.WriteLine($"You have been denied, you have {tries} more tries");
        }

        static void Banned()
        {
            Environment.Exit(0);
        }
    }
}

关键修改点

  • 把递归的账号验证逻辑改成循环实现,彻底解决递归栈导致的流程无法正常返回问题,同时每次验证前重置尝试次数,保证新的登录流程拥有完整的3次机会。
  • 移除LoginForm末尾多余的Console.ReadLine(),消除不必要的输入阻塞。
  • 调整验证流程的控制逻辑,确保登录成功后直接跳出循环,回到Main方法执行后续的CreateRecipe。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 23:21:04