如何使用&&在“做”中和“同时”表情? [英] How to use && in "do" and "while" expressions?

查看:87
本文介绍了如何使用&&在“做”中和“同时”表情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码应该模仿帐户创建者,并在给出不正确的密码时给用户错误(我最终会达到用户名要求)。如果用户输入了一个不正确的密码,我想循环一些代码,但我似乎无法解决它。任何建议将不胜感激。



我的尝试:



My code is supposed to mimic an account creator and give the user errors when given an improper password (I'll eventually get to Username requirements). I want to loop some code if the user input an improper password but I can't seem to get it down. Any suggestion would be appreciated.

What I have tried:

using System;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            string Username;
            string Password;
            int Zero;
            int CapCharacters;
            int LowCharacters;
            int Digits;
            int Symbols;
            int PassLength;

            Console.WriteLine("Hello user, please follow these instructions to create your new account.\n");
            Console.Write($"Please enter a username to go by: ");
            Username = Console.ReadLine();

            do
            {
                Zero = 0;
                CapCharacters = 0;
                LowCharacters = 0;
                Digits = 0;
                Symbols = 0;

                Console.Write($"\nAlright {Username}, now please enter a password: ");
                Password = Console.ReadLine(); // Password rules: 6-12 chara, 1 cap, 1 low, 1 num, 1 sym.
                PassLength = Password.Length;
                Console.WriteLine("");

                while (Zero < PassLength)
                {
                    if (Password[Zero] >= 'a' && Password[Zero] <= 'z')
                    {
                        LowCharacters++;
                    }

                    else if (Password[Zero] >= 'A' && Password[Zero] <= 'Z')
                    {
                        CapCharacters++;
                    }

                    else if (Password[Zero] >= '0' && Password[Zero] <= '9')
                    {
                        Digits++;
                    }
                    else
                    {
                        Symbols++;
                    }

                    Zero++;
                }


                if (Password.Length < 6)
                {
                    Console.WriteLine("Error: Your Password is too short.");
                }

                if (Password.Length > 12)
                {
                    Console.WriteLine("Error: Your Password is too long.");
                }

                if (CapCharacters < 1)
                {
                    Console.WriteLine("Error: Your Password needs to contain a capital letter.");
                }

                if (LowCharacters < 1)
                {
                    Console.WriteLine("Error: Your Password needs to contain a lowercase letter.");
                }

                if (Digits < 1)
                {
                    Console.WriteLine("Error: Your Password needs to contain a digit.");
                }

                if (Symbols < 1)
                {
                    Console.WriteLine("Error: Your Password needs to contain a symbol.");
                }
            }

            while (Password.Length < 6 && Password.Length > 12 &&
                   CapCharacters < 1 && LowCharacters < 1 &&
                   Digits < 1 && Symbols < 1);

            Console.WriteLine("");
            Console.WriteLine("Congradulations, your account is now set up!");
            Console.WriteLine("");
        }
    }
}

推荐答案

请输入用户名:);
Username = Console.ReadLine();

do
{
Zero = 0 ;
CapCharacters = 0 ;
LowCharacters = 0 ;
Digits = 0 ;
符号= 0 ;

Console.Write(
"Please enter a username to go by: "); Username = Console.ReadLine(); do { Zero = 0; CapCharacters = 0; LowCharacters = 0; Digits = 0; Symbols = 0; Console.Write(


\ nAlright {用户名},现在请输入密码:);
Password = Console.ReadLine(); // 密码规则:6-12 chara,1 cap,1 low,1 num,1 sym。
PassLength = Password.Length;
Console.WriteLine( );

while (零< PassLength)
{
if (密码[零] > = ' a'&& Password [Zero] < = ' z'
{
LowCharacters ++;
}

else if (密码[零] > = ' A'&&密码[零] < = ' Z'
{
CapCharacters ++;
}

else if (密码[零] > = ' 0'&&密码[零] < = ' 9'
{
Digits ++;
}
else
{
符号++;
}

零点++;
}


if (Password.Length < 6
{
Console.WriteLine( 错误:您的密码太短。);
}

if (Password.Length > 12
{
Console.WriteLine( 错误:您的密码太长了。);
}

if (CapCharacters < 1
{
Console.WriteLine( 错误:您的密码需要包含大写字母。);
}

if (LowCharacters < 1
{
Console.WriteLine( 错误:您的密码需要包含小写字母。);
}

如果(数字< 1
{
Console.WriteLine( 错误:您的密码需要包含一个数字。);
}

if (符号< 1
{
Console.WriteLine( 错误:您的密码需要包含符号。);
}
}

while (Password.Length < 6 &&& Password.Length > 12 &&
CapCharacters < 1 && LowCharacters < 1 &&
数字< 1 && Symbols < 1 );

Console.WriteLine( );
Console.WriteLine( Congradulations,您的帐户现已设置!);
Console.WriteLine( );
}
}
}
"\nAlright {Username}, now please enter a password: "); Password = Console.ReadLine(); // Password rules: 6-12 chara, 1 cap, 1 low, 1 num, 1 sym. PassLength = Password.Length; Console.WriteLine(""); while (Zero < PassLength) { if (Password[Zero] >= 'a' && Password[Zero] <= 'z') { LowCharacters++; } else if (Password[Zero] >= 'A' && Password[Zero] <= 'Z') { CapCharacters++; } else if (Password[Zero] >= '0' && Password[Zero] <= '9') { Digits++; } else { Symbols++; } Zero++; } if (Password.Length < 6) { Console.WriteLine("Error: Your Password is too short."); } if (Password.Length > 12) { Console.WriteLine("Error: Your Password is too long."); } if (CapCharacters < 1) { Console.WriteLine("Error: Your Password needs to contain a capital letter."); } if (LowCharacters < 1) { Console.WriteLine("Error: Your Password needs to contain a lowercase letter."); } if (Digits < 1) { Console.WriteLine("Error: Your Password needs to contain a digit."); } if (Symbols < 1) { Console.WriteLine("Error: Your Password needs to contain a symbol."); } } while (Password.Length < 6 && Password.Length > 12 && CapCharacters < 1 && LowCharacters < 1 && Digits < 1 && Symbols < 1); Console.WriteLine(""); Console.WriteLine("Congradulations, your account is now set up!"); Console.WriteLine(""); } } }


在while条件中需要一个OR运算符,而不是AND:

You need an OR operator in the while condition, not an AND:
while (Password.Length < 6 || Password.Length > 12 ||
       CapCharacters < 1 || LowCharacters < 1 ||
       Digits < 1 || Symbols < 1);



希望这会有所帮助。


Hope this helps.


这篇关于如何使用&amp;&amp;在“做”中和“同时”表情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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