密码验证器java:链接布尔值?
几周前,我刚刚开始使用java,我正在尝试制作一个程序来验证密码是否至少包含10个字符,每个字符至少包含一个:大写、小写、数字,并且只包含字母数字字符 下面是我到目前为止的代码。实际上,它比代码更英语化,但我要做的是在一个布尔值下运行几个循环(至少我认为我正在这样做)。你能帮我理解布尔运算是如何工作的吗?如果有可能在一个布尔运算下测试多个条件,就像我下面要做的那样 谢谢密码验证器java:链接布尔值?,java,Java,几周前,我刚刚开始使用java,我正在尝试制作一个程序来验证密码是否至少包含10个字符,每个字符至少包含一个:大写、小写、数字,并且只包含字母数字字符 下面是我到目前为止的代码。实际上,它比代码更英语化,但我要做的是在一个布尔值下运行几个循环(至少我认为我正在这样做)。你能帮我理解布尔运算是如何工作的吗?如果有可能在一个布尔运算下测试多个条件,就像我下面要做的那样 谢谢 public class validatingPassword { public static void main(Strin
public class validatingPassword
{
public static void main(String[] args)
{
String password= isSecurePassword("testcase");
System.out.println(isSecurePassword);
}
public static boolean isSecurePassword(String password)
{
password.charAt(x);
int lengthPassword= password.length();
if (lengthPassword < 10);
return false;
for (int x = 0; x < lengthPassword; x++)
{
if ('A' <=x && x <= 'Z');}
else if
return false;
for (int x = 0; x< lengthPassword; x++)
{
('a' <=x && x <= 'z'); }
else if
return false:
for (int x = 0; x < lengthPassword; x++)
{
('0' <=x && x <= '9');}
return true;
}
else if
{
x++;
return false;
}
}
}
公共类验证密码
{
公共静态void main(字符串[]args)
{
字符串密码=isSecurePassword(“testcase”);
System.out.println(isSecurePassword);
}
公共静态布尔isSecurePassword(字符串密码)
{
密码.charAt(x);
int lengthPassword=password.length();
如果(长度密码<10);
返回false;
用于(int x=0;x 如果('A'是的,则可以嵌套布尔语句……不过大括号都搞乱了
你有:
for (int x = 0; x < lengthPassword; x++){
if ('A' <=x && x <= 'Z');}
else if {
return false;
for(int x=0;x if('A'我只是给出了正确的代码,没有文档记录。因为我认为您的想法是正确的,但是缺少一点Java语法。您可以通过单步调试所有命令来调试代码
public class ValidatingPassword {
public static void main(String[] args) {
String[] passwords = { "testcase", "T3stCas3%45" };
for (String password : passwords) {
System.out.println(password + " : " + isSecurePassword(password));
}
}
/**
* Is this a secure password?
* At least 10 characters, at least one capital letter, one small
* letter and one digit.
*
* @param password never null.
* @return whether password is secure.
*/
public static boolean isSecurePassword(String password) {
int lengthPassword = password.length();
if (lengthPassword < 10) {
return false;
}
boolean hasCapital = false;
boolean hasSmallLetter = false;
boolean hasDigit = false;
for (int i = 0; i < lengthPassword; i++) {
char ch = password.charAt(i);
if ('A' <= ch && ch <= 'Z') {
hasCapital = true;
}
if ('a' <= ch && ch <= 'z') {
hasSmallLetter = true;
}
if ('0' <= ch && ch <= '9') {
hasDigit = true;
}
}
return hasCapital && hasSmallLetter && hasDigit;
}
}
公共类验证密码{
公共静态void main(字符串[]args){
字符串[]密码={“testcase”,“T3stCas3%45”};
用于(字符串密码:密码){
System.out.println(密码+”:“+isSecurePassword(密码));
}
}
/**
*这是安全密码吗?
*至少10个字符,至少一个大写字母,一个小写字母
*字母和一个数字。
*
*@param password从不为空。
*@return密码是否安全。
*/
公共静态布尔isSecurePassword(字符串密码){
int lengthPassword=password.length();
如果(长度密码<10){
返回false;
}
布尔hasCapital=false;
布尔字母=false;
布尔hasDigit=false;
对于(int i=0;i 如果('A'上面的代码甚至无法编译,请在澄清您的问题之前纠正所有错误:)我知道它无法编译…我只是不知道如何格式化(即什么是和不允许的),我发现最大的问题是弄清楚java语法。括号让我头疼。我想我需要开始去教授的所有办公时间:Pthanks,但我仍然感到困惑。当你评论//什么都不做时,你是说代码什么都不做,然后继续下一个循环检查小写,然后是数字等等,还是说那是我的“如果”语句本身没有做任何事情?它们只是注释,意味着如果if语句的条件为真,它将直接跳到下一个if语句并检查该语句。最好检查if语句中的否定条件,然后从那里返回,省去了对else的需要,但为了简洁起见,I jusI don’我知道我的java语法很糟糕,所以最好阅读正确编写的代码,并解释如何使用短语
for (int x = 0; x < lengthPassword; x++)
{
if ('A' <=x && x <= 'Z') {
// Do nothing
}
else {
return false;
}
if ('a' <=x && x <= 'z') {
// Do nothing
}
else {
return false;
}
if ('0' <=x && x <= '9') {
// Do nothing
}
else {
return false;
}
}
public class ValidatingPassword {
public static void main(String[] args) {
String[] passwords = { "testcase", "T3stCas3%45" };
for (String password : passwords) {
System.out.println(password + " : " + isSecurePassword(password));
}
}
/**
* Is this a secure password?
* At least 10 characters, at least one capital letter, one small
* letter and one digit.
*
* @param password never null.
* @return whether password is secure.
*/
public static boolean isSecurePassword(String password) {
int lengthPassword = password.length();
if (lengthPassword < 10) {
return false;
}
boolean hasCapital = false;
boolean hasSmallLetter = false;
boolean hasDigit = false;
for (int i = 0; i < lengthPassword; i++) {
char ch = password.charAt(i);
if ('A' <= ch && ch <= 'Z') {
hasCapital = true;
}
if ('a' <= ch && ch <= 'z') {
hasSmallLetter = true;
}
if ('0' <= ch && ch <= '9') {
hasDigit = true;
}
}
return hasCapital && hasSmallLetter && hasDigit;
}
}