使用布尔值检测小写字母。结果总是正确的。
Using boolean to detect lower cases. Outcome always true.
我正在尝试创建一个代码来读取密码,如果密码包含小写字母,则布尔值应为 false 并继续执行其余代码。这是代码片段:
import java.util.Scanner;
public class test
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter a password that meets the following rules: \n\n");
System.out.print("8 characteres long\n");
System.out.print("Contains at least 1 lower letter\n");
System.out.print("Contains at least 1 upper letter\n");
System.out.print("Contains at least 1 numeric digit\n");
System.out.print("Contains 1 special character from "
+ "the set: !@#$%^&*\n");
System.out.print("Does not contain the word \"and\" "
+ "or the word \"end\"\n\n");
String password = stdIn.nextLine();
boolean hasLower;
hasLower = true;
for(int i = 0; (i<password.length()) && (!hasLower); i++){
if(Character.isLowerCase(password.charAt(i))){
hasLower = false;
}
}
System.out.println(hasLower);
} //end main
} // end class
你在for循环中的逻辑不正确。
由于您将 hasLower
变量初始化为真,for 循环中的条件永远不会为真,因此 for 循环永远不会 运行:
(i<password.length()) && (!hasLower)
// !hasLower will not be true since the variable is initialised to true, whereas it should be initialised to false.
因此,要修复代码,请将 hasLower
变量初始化为 false,然后在找到小写字符时退出循环。
hasLower = false;
for(int i = 0; i < password.length(); i++){
if(Character.isLowerCase(password.charAt(i))){
hasLower = true;
break; // Exits the last-entered loop.
}
}
按照您的逻辑,它将跳过 for 循环。请将第二个 && 运算符更改为 (hasLower)。注意:不需要这些括号。
你可能有如下内容
for (int i = 0; i < password.length() && hasLower; i++) {
if (Character.isLowerCase(password.charAt(i))) {
hasLower = false;
}
}
我正在尝试创建一个代码来读取密码,如果密码包含小写字母,则布尔值应为 false 并继续执行其余代码。这是代码片段:
import java.util.Scanner;
public class test
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter a password that meets the following rules: \n\n");
System.out.print("8 characteres long\n");
System.out.print("Contains at least 1 lower letter\n");
System.out.print("Contains at least 1 upper letter\n");
System.out.print("Contains at least 1 numeric digit\n");
System.out.print("Contains 1 special character from "
+ "the set: !@#$%^&*\n");
System.out.print("Does not contain the word \"and\" "
+ "or the word \"end\"\n\n");
String password = stdIn.nextLine();
boolean hasLower;
hasLower = true;
for(int i = 0; (i<password.length()) && (!hasLower); i++){
if(Character.isLowerCase(password.charAt(i))){
hasLower = false;
}
}
System.out.println(hasLower);
} //end main
} // end class
你在for循环中的逻辑不正确。
由于您将 hasLower
变量初始化为真,for 循环中的条件永远不会为真,因此 for 循环永远不会 运行:
(i<password.length()) && (!hasLower)
// !hasLower will not be true since the variable is initialised to true, whereas it should be initialised to false.
因此,要修复代码,请将 hasLower
变量初始化为 false,然后在找到小写字符时退出循环。
hasLower = false;
for(int i = 0; i < password.length(); i++){
if(Character.isLowerCase(password.charAt(i))){
hasLower = true;
break; // Exits the last-entered loop.
}
}
按照您的逻辑,它将跳过 for 循环。请将第二个 && 运算符更改为 (hasLower)。注意:不需要这些括号。
你可能有如下内容
for (int i = 0; i < password.length() && hasLower; i++) {
if (Character.isLowerCase(password.charAt(i))) {
hasLower = false;
}
}