【问题标题】:How to make number Count in loop in c#?如何在c#中循环计数?
【发布时间】:2016-05-18 03:34:08
【问题描述】:

这是一个带有 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;
        }
    }
}

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

【问题讨论】:

  • 永远不要以这种方式使用goto!你可以用ifwhiledo ... whileforswitchtry ... catch...做任何事情

标签: c# loops for-loop while-loop console-application


【解决方案1】:

一个while循环就足够了。使用布尔值检测成功的密码输入。 输入后,它将跳出循环。 无效尝试将减少 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();
        }
    }

【讨论】:

  • 这种方法行不通,因为当您编写成功的用户名并输入错误的密码时会发生什么。由于没有 else 语句,因此您不能认为该尝试是错误的。 Keethri 和 PLC 初学者是唯一用逻辑解决这个问题的人
【解决方案2】:

试试这个更新后的 main 方法:

static void Main(string[] args)
        {
            demo obj = new demo();
            int n = 5;
            string uname, pass;
            Console.ForegroundColor = ConsoleColor.Green;
            //Console.Clear();
        label1:
            Console.WriteLine("\n");
            Console.WriteLine("Enter username");
            uname = Console.ReadLine();
            Console.WriteLine("Enter Password");
            pass = Console.ReadLine();
            obj.setName(uname);
            obj.setPass(pass);
            if (obj.getName() == "niit" && obj.getPass() == "1234")
            {
                    Console.WriteLine("welcome");
            }
            else
            {
                //Console.Clear();
                if (n < 1)
                {
                    //Add ur screenlock n hang prog code 
                    Console.Clear();
                    Console.WriteLine("ScreenLock");
                }
                else
                {

                    Console.WriteLine("\n Invalid");
                    Console.WriteLine("\n To try again enter y");
                    string yes = Console.ReadLine();
                    Console.WriteLine("\n");
                    if (yes == "y")
                    {
                        while (n >= 1)
                        {
                            Console.Write(n + " Tries left");
                            n = --n;
                            goto label1;


                        }
                    }
                }
            }

            Console.ReadKey();

        }

【讨论】:

    【解决方案3】:
    namespace ConsoleApplication4
    {
    class Program
    {
        static void Main(string[] args)
        {
            demo obj = new demo();
            string uname, pass;
            boolean successful = false;
            int32 tries = 5;
            Console.ForegroundColor = ConsoleColor.Green;
        label1:
            Do 
            {
            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");
                    successful = true;
                }
            }
            if (!successful)
            {
                tries--;
                Console.Clear();
                Console.WriteLine("Invalid");
                if (tries > 1)
                { 
                   Console.WriteLine("Have " + tries + " attempts left");
                }
                ElseIf (tries == 1)
                {
                   Console.WriteLine("Have only one more attempt left");
                }
                Else
                {
                   Console.WriteLine("Maximum number of tries exceed");
                   Console.WriteLine("Goodbye");
                }
            }
            } While(!successful && Tries > 0);
        }
    }
    

    【讨论】:

      【解决方案4】:

      每次输入错误时,都会重新计算int n = 5; 所以每次你还有 5 次尝试。

      您可以做的是在static void Main(string args[]) 方法之外声明您的计数

      就像:

      int n =5;
      static void Main(string args[])
      {
      
      }
      

      【讨论】:

        【解决方案5】:

        您的问题是以下嵌套的if-contions

        if (obj.getName() == "niit")
        {
            if (obj.getPass() == "1234")
            {
                Console.WriteLine("welcome");
            }
        }
        else
        {
        \\...
        }
        

        如果用户名正确而pass不正确,则不会进入else分支。 一个更好的解决方案来要求输入直到它是有效的 id a do ... while 循环 下面的例子比你的有很多改进。

        static void Main(string[] args)
        {
            demo obj = new demo();
            string uname, pass;
            Console.ForegroundColor = ConsoleColor.Green;
            int maxTries;
            int tries = maxTries = 5;
            do
            {
                if (tries != maxTries)//second and more
                {
                    Console.Clear();
                    Console.WriteLine("Invalid");
                    Console.Write("\n\t" + tries + " Tries left");
                    Console.WriteLine("\n\n\n\tTry again? (y/n)");
                    string input;
                    do
                    {
                        input = Console.ReadLine();
                    } while (input != "y" && input != "n");
        
                    if (input == "n")
                    {
                        return; // exit the program
                    }
                }
        
                Console.Clear();
                Console.WriteLine("Enter username");
                uname = Console.ReadLine();
                Console.WriteLine("Enter Password");
                pass = Console.ReadLine();
                obj.setName(uname);
                obj.setPass(pass);
                tries--;
            } while (obj.getName() != "niit" || obj.getPass() != "1234");
            Console.WriteLine("Wellcome");
        }
        

        PS:类应以大写字母开头。

        goto 是旧时代的产物,它会弄乱你的程序结构并使事情变得比现在更复杂。我知道的唯一正确用途是用于切换开关,这也仅在极少数情况下需要。

        虚构的应该是:

        string vehicleType = "car";
        switch(vehicleType)
        {
            case "truck":
                Console.WriteLine("two wheeles and");
                goto case "car";
            case "car":
                Console.WriteLine("two wheeles and");
                goto case "motor cycle";
            case "motor cycle":
                Console.WriteLine("two wheeles");
                break;
            case "boat":
                Console.WriteLine("no wheeles");
                break;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-12-07
          • 2020-09-27
          • 2014-03-02
          • 2022-08-14
          • 2011-05-19
          • 1970-01-01
          • 1970-01-01
          • 2015-11-13
          • 2021-09-09
          相关资源
          最近更新 更多