2013-11-27 48 views
-5

制作一个程序,告诉您密码是否符合要求。 要求;密码要求C#WPF

它需要是7个字母或更多。 第一个字母必须是B. 密码必须至少有3个数字。 密码必须至少有1个字母。

问:我该如何解决这个问题?一直使用谷歌没有运气。我不知道如何设定要求。

Lykilord是指密码。 这是我迄今所做的:

private void btathuga_Click(object sender, RoutedEventArgs e) 
    { 
     int lykilord = 0; 
     if (lykilord > 7) 
     { 
      MessageBox.Show("Wrong!"); 
     } 
     else if (true) 
     { 
      MessageBox.Show("Wrong!"); 
     } 
+0

什么是工作?什么不起作用?你有什么具体的问题?例如,你需要帮助检查数字还是字母?计算各种输入类别?说实话,这看起来像一个家庭作业,你的问题听起来有点像“请为我做我的作业”。 – chwarr

+2

我希望这是作业。这些规则非常愚蠢。 – CodesInChaos

+1

您的问题既不明确,也不是您的代码。 – manish

回答

2

设密码是你从用户作为其密码得到的字符串。那么你会接受它,如果

if(password.Length>=7 && 
    password[0]=='B' && 
    password.Where(x => Char.IsDigit(x)).Count()>=3 && 
    password.Where(x => Char.IsLetter(x)).Count()>1) 
{ 
    // The password is ok. Here goes your code. 
} 
0

你有一些要求 - 我将它们分开。

  • 7个字母或更多

您可以通过使用

lykilord.Length(); 
  • 第一个字母必须是B

    lykilord.Substring(0,1); 
    
  • 密码必须至少有3回本号码

这里有几个选择。如果您正在使用C#我会使用LINQ,你可以做

int countOfNumbers = lykilord.Count(Char.IsDigit); 
  • 密码必须至少有1个字母

显然,如果在开始的B,则是一个字母,如果没有,那么它是无效的。但是,如果你想反正检查字母(备查)

int countOfLetters = lykilord.Count(Char.IsLetter); 

使用所有的这些结合在一起,你可以编译的,如果这样的语句:

if (lykilord.Substring(0,1).ToUpper() == "B" && // the ToUpper() will mean they can have either lowercase or uppercase B 
     lykilord.Length() > 7 && 
     countOfNumbers > 3 &&) 
     // i've excluded the letter count as you require the B 
    { 
     // password is good 
    } 
    else 
    { 
     // password is bad 
    } 

我已经做了我最好的分裂它可以让你从中学习 - 它看起来有点像作业或学术任务的一部分。我们不在这里为你做这个作业。

1

这是我从你的问题推断,可能是这将有助于

private void button1_Click(object sender, EventArgs e) 
{ 
    if (textBox1.Text.Length < 7) 
     { 
      MessageBox.Show("Password can't be less than 7 letter long !"); 
      return; 
     } 
     else if (!textBox1.Text.ToString().Substring(0, 1).Equals("B")) 
     { 
      MessageBox.Show("Password's first letter must be 'B'"); 
      return; 
     } 
     int countOfDigit = 0, countOfLetters = 0; 
     foreach (var digit in textBox1.Text) 
     { 
      if (char.IsDigit(digit)) 
       ++countOfDigit; 
      if (char.IsLetter(digit)) 
       ++countOfLetters; 
     } 
     if (countOfDigit < 3) 
     { 
      MessageBox.Show("Password must have atleast 3 digits"); 
      return; 
     } 

     if (countOfLetters < 1) 
     { 
      MessageBox.Show("Password must have atleast 1 Letter"); 
      return; 
     } 
     MessageBox.Show("Congratulations ! Your Password is as per Norms !"); 
    } 
+0

'goto'!使用'return;'代替。 (相关[SO问题](http://stackoverflow.com/questions/46586/goto-still-considered-有害)和[XKCD](http://xkcd.com/292/))。 – Nasreddine

+0

先生,@Nasreddine我很欣赏你的评论,但如果我使用返回,我不能方便地向用户显示恭喜消息。这就是为什么。否则,我将不得不使用冗余代码。 – manish

+0

是的,没有任何“冗余代码”是可能的。只需将'goto END;'的每个实例替换为'return;'并删除标签'END:;'。 – Nasreddine