如何使我的if陈述较少矛盾?

发布于 2025-02-12 02:30:10 字数 1384 浏览 1 评论 0原文

我有一个正在处理密码强度检查器的积分系统。

当前,规则像以下内容一样工作:+5如果用户条目的案例字母较低,则+10如果其具有较低和上限的字母,数字或特殊字符等。

我遇到的问题是,我的if语句是矛盾的,如果我可以使用任何替代方案,我正在徘徊。

下面的陈述是我试图获得它,因此,如果他们放置了一定类型的角色,他们将获得积分,并且它将有助于分数,即如果有数字,则得分为+5。

if (userPassword.Any(char.IsDigit))
{
    points += 5;
}

if (userPassword.Any(char.IsLower))
{
    points += 5;
}

if (userPassword.Any(char.IsUpper))
{
    points += 5;
}

if (userPassword.Any(c => specialCharacters.Contains(c)))
{
    points += 5;
}

为了摆脱矛盾,我试图在可能扣除用户点的语句上执行所有功能,但似乎不足,我认为这对于该程序并不适合。

如果用户仅使用一种类型的字符,我基本上是在尝试将其扣除点,即如果他们只使用数字而没有其他东西会失去积分。

if (userPassword.All(char.IsLower) || userPassword.All(char.IsUpper)) 
{
    points -= 5;
}

if (userPassword.All(char.IsDigit)) 
{
    points -= 5;
}

if (userPassword.All(c => specialCharacters.Contains(c))) 
{
    points -= 5;
}

解决这个问题是什么选择?

编辑: 好的,谢谢您澄清我的if语句没有任何问题。现在,我想添加一个IF语句,如果其中包含大型情况,较低的情况,数字和特殊字符,则添加10点。我试图使用.contain语句尝试并将其与其他添加if语句区分开用于绕过它。

if (userPassword.Contains(char.IsUpper) &&
    userPassword.Contains(char.IsLower) &&
    userPassword.Contains(char.IsDigit) &&
    userPassword.Contains(c => specialCharacters.Contains(c)))
{
    points += 10;
}

I have a points system for a password strength checker that I am working on.

Currently the rules work like something this: +5 if the user entry has a lower case letter, +10 if it has a lower and upper case letter, a number or a special character ,etc.

The problem I have ran into is that my if statements are contradictory and I was wandering if there were any alternatives I could use.

The statements below is me trying to get it so if they put in a certain type of character, they will gain points and it will contribute to the score, i.e. if it has a number, then +5 to the score.

if (userPassword.Any(char.IsDigit))
{
    points += 5;
}

if (userPassword.Any(char.IsLower))
{
    points += 5;
}

if (userPassword.Any(char.IsUpper))
{
    points += 5;
}

if (userPassword.Any(c => specialCharacters.Contains(c)))
{
    points += 5;
}

To get rid of the contradiction, I tried to do an .all function on the statements that potentially deduct points of the user, but it seems inadequate and I don't think it is quite right for this programme.

I am basically trying to get it to deduct points if the user is using only one type of character, i.e. if they only use numbers and nothing else they will lose points.

if (userPassword.All(char.IsLower) || userPassword.All(char.IsUpper)) 
{
    points -= 5;
}

if (userPassword.All(char.IsDigit)) 
{
    points -= 5;
}

if (userPassword.All(c => specialCharacters.Contains(c))) 
{
    points -= 5;
}

What would be an alternative to solve this?

EDIT:
Ok thank you for clarifying that there isn't anything wrong with my if statements. I now want to add an if statement that adds 10 points if it contains an upper case, lower case, digit and special character. I have tried to use the .contain statement to try and differentiate it from the other addition if statements but it is giving me an error of Argument 1: Cannot Convert from 'method group' to 'string'and I don't know what to use to bypass it.

if (userPassword.Contains(char.IsUpper) &&
    userPassword.Contains(char.IsLower) &&
    userPassword.Contains(char.IsDigit) &&
    userPassword.Contains(c => specialCharacters.Contains(c)))
{
    points += 10;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2025-02-19 02:30:10

如果我正确理解,则需要适合“所有大写”等有条件的密码来减去点,同时也没有添加单个大写的积分。像目前一样,在您的示例中,“ ABC”将获得+5,因为一个小写,然后将所有小写的小写损失为5,而晚上为0,而不是-5。

我会在开始时将您的点递减条件放入单个IF语句中,然后将您的所有积分添加条件都放在if语句中,以在其他语句中:

if (userPassword.All(char.IsLower) ||
  userPassword.All(char.IsUpper) ||
  userPassword.All(char.IsDigit) ||
  userPassword.All(c => specialCharacters.Contains(c)) {
    points -= 5;
  } else {
      if (userPassword.Any(char.IsDigit)) {
        points += 5;
      }
      if (userPassword.Any(char.IsLower)) {
        points += 5;
      }
      if (userPassword.Any(char.IsUpper)) {
        points += 5;
      }
      if (userPassword.Any(c => specialCharacters.Contains(c))) {
        points += 5;
      }
    }

另外,如果您需要分开这些点赋予的条件以允许不同条件下的点扣除值的不同值,请使用更多的if if语句对不良情况使用:

if (userPassword.All(char.IsLower) || userPassword.All(char.IsUpper)) {
  points -= 5;
} else if (userPassword.All(char.IsDigit)) {
  points -= 5;
} else if (userPassword.All(c => specialCharacters.Contains(c))) {
  points -= 5;
} else {
  if (userPassword.Any(char.IsDigit)) {
    points += 5;
  }
  if (userPassword.Any(char.IsLower)) {
    points += 5;
  }
  if (userPassword.Any(char.IsUpper)) {
    points += 5;
  }
  if (userPassword.Any(c => specialCharacters.Contains(c))) {
    points += 5;
  }
}

If I'm understanding correctly, you want passwords which fit the "all uppercase," et cetera conditionals to subtract points while also NOT adding points for having a single uppercase. Like currently, "abc" in your example would receive +5 for having one lowercase, then would lose 5 for having all lowercase, evening out to 0 rather than -5.

I would put your point-deducting conditionals into a single if statement at the beginning, then put all of your point-adding conditionals into if statements within an else statement:

if (userPassword.All(char.IsLower) ||
  userPassword.All(char.IsUpper) ||
  userPassword.All(char.IsDigit) ||
  userPassword.All(c => specialCharacters.Contains(c)) {
    points -= 5;
  } else {
      if (userPassword.Any(char.IsDigit)) {
        points += 5;
      }
      if (userPassword.Any(char.IsLower)) {
        points += 5;
      }
      if (userPassword.Any(char.IsUpper)) {
        points += 5;
      }
      if (userPassword.Any(c => specialCharacters.Contains(c))) {
        points += 5;
      }
    }

Alternatively, if you needed those point-deducting conditionals to be separated to allow for different values of point deductions for different conditions, just use more else if statements for the bad ones:

if (userPassword.All(char.IsLower) || userPassword.All(char.IsUpper)) {
  points -= 5;
} else if (userPassword.All(char.IsDigit)) {
  points -= 5;
} else if (userPassword.All(c => specialCharacters.Contains(c))) {
  points -= 5;
} else {
  if (userPassword.Any(char.IsDigit)) {
    points += 5;
  }
  if (userPassword.Any(char.IsLower)) {
    points += 5;
  }
  if (userPassword.Any(char.IsUpper)) {
    points += 5;
  }
  if (userPassword.Any(c => specialCharacters.Contains(c))) {
    points += 5;
  }
}

护你周全 2025-02-19 02:30:10

将您的“矛盾”检查与相关的“确认”检查结合在一起。例如,在测试包含数字的测试时,还要确保它们不是全部数字:

if( userPassword.Any(char.isDigit)
    && !userPassword.All(char.isDigit) )
{
    points += 5;
}
else /*if(userPassword.All(char.isDigit)*/ // if desired
{
    points -= 5; // if appropriate
}

现在让我们讨论重组您的算法。我们可以创建一个可以用来定义检查的类,而不是定义的长链,而语句,

public class PasswordEvaluator
{
    public Func<string, bool> TestCondition { get; init set; }
    public int AffirmativeResultValue { get; init set; }
    public int NegativeResultValue { get; init set; }

    public int Evaluate(string password) =>
        TestCondition(password)
            ? AffirmativeResultValue 
            : NegativeResultValue ;
}

则可以定义测试。使用上述解决方案:

var hasDigitEvaluator = new PasswordEvaluator
{
    Condition = (string password) => password.Any(char.isDigit)
        && !password.All(char.isDigit),
    AffirmativeResultValue = 5,
    NegativeResultValue = -5, // or 0, whatever
};

然后,您可以使用此对象执行操作:

points += hasDigitEvaluator.Evalutate(userPassword);

现在,要处理多个评估器,您可以组合iEnumerable&lt; passwordEvaluator&gt;,然后在其上调用单个操作以获取您的聚合结果:

var passwordEvaluators = new[]
{
    new PasswordEvaluator { ... },
    new PasswordEvaluator { ... },
    ...
};

var points = passwordEvaluators
    .Select(test => test.Evaluate(userPassword))
    .Sum();

Combine your "contradiction" check with the relevant "affirmation" check. For example, when testing for containing a digit, also ensure they're not all digits:

if( userPassword.Any(char.isDigit)
    && !userPassword.All(char.isDigit) )
{
    points += 5;
}
else /*if(userPassword.All(char.isDigit)*/ // if desired
{
    points -= 5; // if appropriate
}

Now lets discuss restructuring your algorithm. Instead of defining long chains of if or if/else statements, let's create a class you can use to define your checks:

public class PasswordEvaluator
{
    public Func<string, bool> TestCondition { get; init set; }
    public int AffirmativeResultValue { get; init set; }
    public int NegativeResultValue { get; init set; }

    public int Evaluate(string password) =>
        TestCondition(password)
            ? AffirmativeResultValue 
            : NegativeResultValue ;
}

Then you can define your tests. Using the above solution:

var hasDigitEvaluator = new PasswordEvaluator
{
    Condition = (string password) => password.Any(char.isDigit)
        && !password.All(char.isDigit),
    AffirmativeResultValue = 5,
    NegativeResultValue = -5, // or 0, whatever
};

Then you can use this object to perform the operation:

points += hasDigitEvaluator.Evalutate(userPassword);

Now, to handle multiple evaluators, you can compose an IEnumerable<PasswordEvaluator> and then call a single operation over it to get your aggregate result:

var passwordEvaluators = new[]
{
    new PasswordEvaluator { ... },
    new PasswordEvaluator { ... },
    ...
};

var points = passwordEvaluators
    .Select(test => test.Evaluate(userPassword))
    .Sum();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文