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

C#控制台程序实现登出返回登录页并重置登录次数的方案

C# 控制台程序实现登出返回登录页功能

需求说明

作为C#新手,需完成学校项目的以下功能:

  • 用户登出时,程序不关闭,返回登录页面
  • 重置登录尝试次数为0
  • 重置登录相关数据

原代码使用Environment.Exit、System.Exit或Application.Restart均无法满足需求,以下是修改后的完整解决方案:

修改后的代码

using System;

namespace GIMPIES1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 库存变量放在外层,登出后保留数据
            int aantal1 = 0;
            int aantal2 = 0;
            int aantal3 = 0;
            int aantal4 = 0;

            // 外层循环控制整个程序,登出后回到登录
            while (true)
            {
                // 每次进入登录环节,重置登录尝试次数
                int loginAttempts = 0;
                bool loginSuccess = false;
                string inputlogin = "";
                string inputpassword = "";
                string login = "inkoop";
                string password = "gimpies_inkoop";

                // 登录尝试循环,最多3次
                while (loginAttempts < 3)
                {
                    Console.Clear();
                    Console.WriteLine("Voeg je gebruikersnaam in.");
                    inputlogin = Console.ReadLine();
                    Console.WriteLine("Voeg wachtwoord in");
                    inputpassword = Console.ReadLine();

                    if (inputlogin == login && inputpassword == password)
                    {
                        Console.Clear();
                        Console.WriteLine("Inloggen gelukt");
                        Console.WriteLine("Klik op enter om door te gaan");
                        Console.ReadLine();
                        loginSuccess = true;
                        break;
                    }
                    else
                    {
                        loginAttempts++;
                        Console.Clear();
                        Console.WriteLine($"Inloggen niet gelukt. Nog {3 - loginAttempts} pogingen over.");
                        Console.ReadKey();
                    }
                }

                // 3次登录失败,退出程序
                if (!loginSuccess)
                {
                    Console.Clear();
                    Console.WriteLine("Programma gaat afsluiten u heeft al uw pogingen gebruikt.");
                    Console.ReadKey();
                    break;
                }

                // 登录成功后进入菜单循环,直到用户登出
                bool isLoggedOut = false;
                while (!isLoggedOut)
                {
                    isLoggedOut = Menu(aantal1, aantal2, aantal3, aantal4, out aantal1, out aantal2, out aantal3, out aantal4);
                }
            }
        }

        // Menu方法返回是否登出,通过out参数更新库存
        static bool Menu(int aantal1, int aantal2, int aantal3, int aantal4, out int updatedAantal1, out int updatedAantal2, out int updatedAantal3, out int updatedAantal4)
        {
            updatedAantal1 = aantal1;
            updatedAantal2 = aantal2;
            updatedAantal3 = aantal3;
            updatedAantal4 = aantal4;
            bool logout = false;

            Console.Clear();
            Console.WriteLine("1. Voorraad schoenen bekijken");
            Console.WriteLine("2. Schoenen inkopen");
            Console.WriteLine("3. uitloggen");
            Console.WriteLine("Maak uw menukeuze en klik op ENTER");
            
            // 处理非数字输入,避免程序崩溃
            if (!int.TryParse(Console.ReadLine(), out int schoenvoorraad))
            {
                Console.WriteLine("Ongeldige keuze, klik op enter om terug te gaan.");
                Console.ReadLine();
                return logout;
            }

            Console.Clear();
            switch (schoenvoorraad)
            {
                case 1:
                    Console.WriteLine("1. Nike, Airmax");
                    Console.WriteLine("2. Nike, Jordan");
                    Console.WriteLine("3. Adidas, yeezys");
                    Console.WriteLine("4. Converse, Allstars");

                    Console.Write("Kies je schoenensoort: ");
                    if (int.TryParse(Console.ReadLine(), out int keuze))
                    {
                        switch (keuze)
                        {
                            case 1:
                                Console.WriteLine("Merk. Nike");
                                Console.WriteLine("Type. Air Max");
                                Console.WriteLine("Maat. 42");
                                Console.WriteLine("Kleur. Rood");
                                Console.WriteLine("Aantal " + aantal1);
                                Console.WriteLine("Prijs. 50,00");
                                break;
                            case 2:
                                Console.WriteLine("Merk. Nike");
                                Console.WriteLine("Type. Jordan");
                                Console.WriteLine("Maat. 41");
                                Console.WriteLine("Kleur.Zwart");
                                Console.WriteLine("aantal " + aantal2);
                                Console.WriteLine("Prijs. 140");
                                break;
                            case 3:
                                Console.WriteLine("Merk. Adidas");
                                Console.WriteLine("Type. Yeezys");
                                Console.WriteLine("Maat. 40");
                                Console.WriteLine("Kleur. Grijs");
                                Console.WriteLine("aantal " + aantal3);
                                Console.WriteLine("Prijs 230");
                                break;
                            case 4:
                                Console.WriteLine("Merk. Converse");
                                Console.WriteLine("Type. Allstars");
                                Console.WriteLine("Maat. 43");
                                Console.WriteLine("Kleur. Wit");
                                Console.WriteLine("aantal " + aantal4);
                                Console.WriteLine("Prijs 165");
                                break;
                            default:
                                Console.WriteLine("Ongeldige keuze!");
                                break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Ongeldige invoer!");
                    }
                    break;

                case 2:
                    Console.WriteLine("1. Nike, Airmax");
                    Console.WriteLine("2. Nike, Jordan");
                    Console.WriteLine("3. Adidas, yeezys");
                    Console.WriteLine("4. Converse, Allstars");
                    Console.WriteLine("Kies de schoenen dat je wilt kopen");
                    
                    if (int.TryParse(Console.ReadLine(), out int inkoop))
                    {
                        switch (inkoop)
                        {
                            case 1:
                                Console.WriteLine($"Hoeveel aantal schoenen wil je inkopen? Huidig aantal: {aantal1}");
                                if (int.TryParse(Console.ReadLine(), out int add1))
                                {
                                    updatedAantal1 += add1;
                                }
                                else
                                {
                                    Console.WriteLine("Ongeldig aantal!");
                                }
                                break;
                            case 2:
                                Console.WriteLine($"Hoeveel aantal schoenen wil je inkopen? Huidig aantal: {aantal2}");
                                if (int.TryParse(Console.ReadLine(), out int add2))
                                {
                                    updatedAantal2 += add2;
                                }
                                else
                                {
                                    Console.WriteLine("Ongeldig aantal!");
                                }
                                break;
                            case 3:
                                Console.WriteLine($"Hoeveel aantal schoenen wil je inkopen? Huidig aantal: {aantal3}");
                                if (int.TryParse(Console.ReadLine(), out int add3))
                                {
                                    updatedAantal3 += add3;
                                }
                                else
                                {
                                    Console.WriteLine("Ongeldig aantal!");
                                }
                                break;
                            case 4:
                                Console.WriteLine($"Hoeveel aantal schoenen wil je inkopen? Huidig aantal: {aantal4}");
                                if (int.TryParse(Console.ReadLine(), out int add4))
                                {
                                    updatedAantal4 += add4;
                                }
                                else
                                {
                                    Console.WriteLine("Ongeldig aantal!");
                                }
                                break;
                            default:
                                Console.WriteLine("Ongeldige keuze!");
                                break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Ongeldige invoer!");
                    }
                    break;

                case 3:
                    Console.WriteLine("Uitloggen...");
                    logout = true;
                    break;

                default:
                    Console.WriteLine("Ongeldige keuze, klik op enter om terug te gaan.");
                    break;
            }

            if (!logout)
            {
                Console.WriteLine("Klik op enter om terug te gaan naar het menu.");
                Console.ReadLine();
            }
            return logout;
        }
    }
}

关键修改说明

  • 外层循环控制流程:用while(true)包裹整个程序逻辑,登出后自动回到登录环节
  • 登录次数重置:每次进入登录流程时,loginAttempts被重置为0,满足登出后重置需求
  • Menu方法返回登出状态:通过返回bool值标记用户是否选择登出,主程序据此退出菜单循环,回到登录页
  • 库存数据持久化:库存变量移至主循环外层,避免登出后丢失已修改的库存数据
  • 输入验证优化:添加int.TryParse处理用户输入,避免非数字输入导致程序崩溃

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 15:43:15