Skip to main content
 首页 » 编程设计

c#之如何在 C# 中循环计数

2024年02月05日16webabcd

这是一个带有 setter 和 getter 概念的简单初学者程序 现在我必须先输入用户名和密码才能受到欢迎 如果我输入错误的信息,它应该显示无效和 5 次尝试,然后如果我再次输入错误的信息,它应该显示 4 次尝试,依此类推,最后当所有尝试都结束时,它应该挂起程序或锁定屏幕等等

using System; 
 
 
namespace ConsoleApplication4 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            demo obj = new demo(); 
            string uname, pass; 
            Console.ForegroundColor = ConsoleColor.Green; 
        label1: 
            Console.Clear(); 
            Console.WriteLine("Enter username"); 
            uname = Console.ReadLine(); 
            Console.WriteLine("Enter Password"); 
            pass = Console.ReadLine(); 
            obj.setName(uname); 
            obj.setPass(pass); 
            if (obj.getName() == "niit") 
            { 
                if (obj.getPass() == "1234") 
                { 
                    Console.WriteLine("welcome"); 
 
                } 
            } 
            else 
            { 
                Console.Clear(); 
                Console.WriteLine("Invalid"); 
                Console.WriteLine("\n \n \n To try again enter y"); 
                int n = 5; 
                string yes = Console.ReadLine(); 
                    if (yes == "y") 
                { 
                    while (n >= 1) 
                    { 
                        Console.Write(n + " Tries left"); 
                        goto label1; 
                        n = --n; 
                    } 
                } 
            } 
 
            Console.ReadKey(); 
 
        } 
    } 
    class demo 
    { 
        private string name, pass; 
        public void setName(string name) 
        { 
            this.name = name; 
        } 
        public string getName() 
        { 
            return name; 
        } 
        public void setPass(string pass) 
        { 
            this.pass = pass; 
        } 
        public string getPass() 
        { 
            return pass; 
        } 
    } 
} 

请建议一个简单的初学者代码,使循环工作并进行倒计时

请您参考如下方法:

while 循环就足够了。使用 bool 值检测成功的密码输入。 输入时,它将跳出循环。 无效尝试将减少 AttemptsLeft int。 注意:我没有在 Visual Studio 中尝试过这个,逻辑应该是合理的,但我建议调试并单步执行它以测试它是否符合您的标准。

static void Main(string[] args) 
    { 
        demo obj = new demo(); 
        string uname, pass; 
        Console.ForegroundColor = ConsoleColor.Green; 
    label1: 
        Console.Clear(); 
        Console.WriteLine("Enter username"); 
        uname = Console.ReadLine(); 
        Console.WriteLine("Enter Password"); 
       bool SuccessfulPassword = false; 
       int AttemptsLeft = 5; 
        while(!SuccessfulPassword && AttemptsLeft > 0){ 
        pass = Console.ReadLine(); 
        obj.setName(uname); 
        obj.setPass(pass); 
        if (obj.getName() == "niit") 
        { 
            if (obj.getPass() == "1234") 
            { 
                Console.WriteLine("welcome"); 
                SuccessfulPassword = true; 
            } 
        } 
        else 
        { 
            AttemptsLeft--; 
            Console.Clear(); 
            Console.WriteLine("Invalid"); 
            Console.WriteLine("\n \n \n To try again enter y"); 
            int n = 5; 
            string yes = Console.ReadLine(); 
                if (yes == "y") 
            { 
 
                    Console.Write(AttemptsLeft + " Tries left"); 
            } 
        } 
 
        Console.ReadKey(); 
        } 
    }