用户ID和密码 [英] User id and password

查看:62
本文介绍了用户ID和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static void Main(string[] args)
{
string username = "coffee";
            string password = "tea";

            Console.WriteLine("Enter your name : ");
            username = Console.ReadLine();

            Console.WriteLine("Enter your password : ");
            password = Console.ReadLine();

            for (int i = 0; i <= 2; i++)
            {
                if (username == "coffee" && password == "tea")
                {
                    Console.WriteLine("\n");
                    Console.WriteLine("Welcome mr coffee");
                    Console.ReadLine();
                    break;
                }

                else
                {
                    Console.WriteLine("\n");
                    Console.WriteLine("try again");

                    Console.WriteLine("Enter your name : ");
                    username = Console.ReadLine();

                    Console.WriteLine("Enter your password : ");
                    password = Console.ReadLine();
                    Console.WriteLine("\n");
                }
            }
            Console.WriteLine("Invalid data try again later");
            Console.ReadLine();
}





我的尝试:



这段代码出了什么问题?

答案欢迎先生咖啡正在打印两次。



What I have tried:

what's wrong in this code?
the answer "Welcome mr coffee" is getting print two times.

推荐答案

认为您发布的代码是您使用的代码,据我所知,当密码正确时,它只打印一次...



但是,最后你打印出无效数据稍后再试,即使数据是正确的,所以你可能想用一个布尔标志来识别是否输入了正确的数据。



另一件事是你在两个地方的代码中重复这个问题。为什么不将它放在循环的开头并在问题之后测试数据。
Taken that the code you posted is the one you use, as far as I can see, it prints the only once when the password is correct...

However, in the end you print out "Invalid data try again later" even if the data was correct so you may want to use a boolean flag to identify if correct data has been input.

Another thing is that you repeat the question in your code in two places. Why not place it only in the beginning of the loop and test the data after the question.


你说要打印两次;



You have said to print it twice;

for (int i = 0; i <= 2; i++)





这说,

a)创建一个名为 i 的变量并将值设置为< b>零。

b)执行循环并增加 i 1

c)这样做而 i 小于 2



因此,它完成循环两次,一次为i = 0& ;再次为i = 1



亲切的问候



This says,
a) Create a variable named i and set the value to zero.
b) Execute the loop and increment i by 1
c) Do this while i is less than 2

Hence, it completes the loop twice, once for i = 0 & again for i = 1

Kind Regards


这篇关于用户ID和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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