如何使登录代码仅适用于特定用户? [英] How do I make a login code only work for a certain user?

查看:90
本文介绍了如何使登录代码仅适用于特定用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它告诉我Anders在当前上下文中不存在,而且else项有错误吗?我不明白为什么



我尝试过:



It tells me that "Anders" does not exist in a current context, also that there is an error with the else term? I don't understand why

What I have tried:

using System;
public class login
{
public static void Main()
{
string user;
int pass;
int counter = 0;
do
{
Console.Write("Enter a user: ");
user = (Console.ReadLine());
Console.Write("Enter a password: ");
pass = Convert.ToInt32(Console.ReadLine());
if ((user != Anders) || (pass != 1234))
{
Console.WriteLine("Login Error");
counter++;
}
}
while (((user != "Anders") || (pass != 1234)) && (counter != 3));
if ((user != "Anders") || (pass != 1234));
Console.WriteLine("Logged out!");
else
Console.WriteLine("Login successful");
}
}

推荐答案

从简单的开始:

Start with the easy one:
引用:

else项有错误吗?

there is an error with the else term?

仔细查看你的代码:

Look at your code closely:

if ((user != "Anders") || (pass != 1234));

看到那个分号?

结束 if 块,因此无论 if 条件如何,都会执行以下语句。因此,系统找不到 if 来匹配 else 并抱怨。

删除分号:

See that semicolon?
That ends the if block, so the following statement is executed regardless of the if condition. And because of that, the system can't find an if to match the else and complains.
Remove the semicolon:

if ((user != "Anders") || (pass != 1234))
   {
   Console.WriteLine("Logged out!");
   }
else
   {
   Console.WriteLine("Login successful");
   }

您的错误将消失。

BTW:总是使用花括号是一个非常好的主意,即使你不需要它们 - 如果你添加一条线,它会在以后节省很多麻烦。有了支撑,很明显这条线需要在它们内部,没有?不太清楚,可能导致错误。



其他问题:

And your error will disappear.
BTW: it's a very good idea to always use curly braces, even when you don't need them - it saves a lot of grief later on if you add a line. With the braces, it's obvious that the line need to be inside them, without? Not so clear and can lead to mistakes.

Other problem:

Quote:

Anders在当前上下文中不存在,

"Anders" does not exist in a current context,

正确,但不是。再看一下代码:

Correct, it doesn't. Again, look at the code:

if ((user != Anders) || (pass != 1234))

Anders 未引用,因此它是变量名,而不是一个字符串值。该方法中没有名为 Anders 的变量,因此系统不知道您的意思和抱怨。

稍后在您的代码中,你使用一个字符串:

Anders is not quoted, so it's a variable name, not a string value. There is no variable called Anders in that method, so the system doesn't know what you mean and complains.
Later on in your code, you use a string:

while (((user != "Anders") || (pass != 1234)) && (counter != 3));

所以猜测,你想在 Anders <周围使用双引号/ code>在这两种情况下:

So at a guess, you want to use double quotes around Anders in both cases:

if ((user != "Anders") || (pass != 1234))


使用系统;

公共类登录

{

public static void Main()

{

string user;

int pass;

int counter = 0;







试一试{



登录:

Console.Write(输入用户:);

user =( Console.ReadLine());

Console.Write(输入密码:);

pass = Convert.ToInt32(Console.ReadLine());

if((user!=Anders)|| (传递!= 1234))

{

Console.WriteLine(登录错误);

counter ++;

转到登录;



}

else {

Console.WriteLine(登录成功.. !!)< br $>
}



}

catch(exception ex){

Console.WriteLine (ex.Message);

}

}
using System;
public class login
{
public static void Main()
{
string user;
int pass;
int counter = 0;



try {

Login:
Console.Write("Enter a user: ");
user = (Console.ReadLine());
Console.Write("Enter a password: ");
pass = Convert.ToInt32(Console.ReadLine());
if ((user != "Anders") || (pass != 1234))
{
Console.WriteLine("Login Error");
counter++;
goto Login;

}
else {
Console.WriteLine("Logged in successfully .. !!")
}

}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}


这篇关于如何使登录代码仅适用于特定用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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