我在C#Console中遇到问题 [英] I'm having a problem in C# Console

查看:112
本文介绍了我在C#Console中遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Cosmos中创建一个操作系统。它与C#Console几乎相同。

我遇到登录和命令问题,这是代码

  public   void  login()
{
sign:
string user;
string 密码;
Console.WriteLine( 用户名:);
user = Console.ReadLine();
Console.WriteLine( 密码);
password = Console.ReadLine();
if (user == rojus
如果(密码== dev
{
Console.WriteLine( 已登录);
InSys();
}

else Console.WriteLine( 密码无效!再试一次);
else Console.WriteLine( 用户名无效!再试一次);
goto sign;
}
public void InSys()
{

Console.Write( / TOS / Rojus @:);
string help;
help = Console.ReadLine();
if (help == help
Console.WriteLine( \ nHelp - 显示命令列表);
if (help == logout
login();




}
}
}



每当我尝试登录并做错了,我可以重新登录,这样就好了,但每当我尝试输入帮助时,它会显示帮助 - 显示命令列表然后用户名,我必须重新登录。如果我输入logout,它只是说用户名,我需要重新登录。我尝试删除标志:并转到标志;但是每当我尝试登录并做错时,它会说无效的密码或用户名,我只是进入InSys。我需要它,所以只有当用户名和密码正确并且命令工作正常时它才会进入InSys。



谢谢你,Rojus。

解决方案

你必须建立一个状态机 [ ^ ](在一个永无止境的循环中)。当状态为,例如 WAIT_FOR_LOGIN 时,仅执行登录调用。当状态是,例如 LOGGED_IN 然后你可以解析其他命令,如 help logout

I am trying to make an Operating System in Cosmos. It's pretty much the same as C# Console.
I am having problems with the login and the commands, this is the code

        public void login()
        {
            sign:
            string user;
            string password;
            Console.WriteLine("Username:");
            user = Console.ReadLine();
            Console.WriteLine("Password");
            password = Console.ReadLine();
            if (user == "rojus")
                if (password == "dev")
                {
                    Console.WriteLine("Logged in");
                    InSys();
                }

                else Console.WriteLine("Invalid password! Try again");
            else Console.WriteLine("Invalid username! Try again");
            goto sign;
        }
        public void InSys()
        {
            
            Console.Write("/TOS/Rojus@:");
            string help;
            help = Console.ReadLine();
            if (help == "help")
                Console.WriteLine("\nHelp - Displays a list of commands");
            if (help == "logout")
                login();
                
            
                
 
        }
    }
}


Whenever I try to login and do it incorrectly, I can relogin so that's fine, but whenever I try to type "Help" for example, it says "Help - Displays a list of commands" And then "Username" and I gotta relogin. If I type "logout" it just says Username and I need to relogin. I tried deleting the sign: and goto sign;, but whenever I try to login and do it wrong, it says Invalid password or username and I simply go in the InSys. I need it that so it will go to InSys only if the username and password is correct and the commands to work fine.

Thank you, Rojus.

解决方案

You have to build a state machine[^] (in a never-ending loop). When the state is, say WAIT_FOR_LOGIN then only the login call is performed. When the state is, say LOGGED_IN then you may parse other commands like help or logout.


这篇关于我在C#Console中遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆