Java 我应该使用什么来比较密码应用程序上的前两个输入字符串 我想在一开始就补充一点,如果密码不匹配,重新开始…但每次我尝试这样做,我就会把整个事情搞砸 密码长度必须为8个字符,一个大写字母和一个数字
我遇到的一些问题是,如果第一个密码无效,它仍然会要求第二个确认密码,为什么在第一个密码无效时继续4.我想可能是compareTo()方法或compareChar,因为它可以是数字,.equals?我愿意接受任何其他优化性能的建议和技巧,因为我知道我的代码是草率的 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); 系统输出打印(“请输入密码:”); 字符串password=in.nextLine() } 公共静态布尔值isValid(字符串密码){ 布尔值atleastOneUpper=false; 布尔值atleastOneLower=false; 布尔值至少一位数=false; 如果(password.length()<8){//如果少于8个字符,则其自动无效 返回false; }Java 我应该使用什么来比较密码应用程序上的前两个输入字符串 我想在一开始就补充一点,如果密码不匹配,重新开始…但每次我尝试这样做,我就会把整个事情搞砸 密码长度必须为8个字符,一个大写字母和一个数字,java,Java,我遇到的一些问题是,如果第一个密码无效,它仍然会要求第二个确认密码,为什么在第一个密码无效时继续4.我想可能是compareTo()方法或compareChar,因为它可以是数字,.equals?我愿意接受任何其他优化性能的建议和技巧,因为我知道我的代码是草率的 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); 系统输出打印(“请输入密码:”); 字符串password=in.nextLine() } 公共静态布尔值isValid(字符串密码){ 布尔值at
for(inti=0;i
}
公共静态字符串密码检查(字符串密码){
String result=“Valid Password”//设置有效密码
int length=0;//存储#
int numCount=0;//在密码中存储数字
int capCount=0;//用于在密码中存储大写字母的变量
对于(int x=0;x=47&&Password.charAt(x)=64&&Password.charAt(x)=97&&Password.charAt(x)47&&Password.charAt(x)<58)){//
numCount++;
}
if((Password.charAt(x)>64&&Password.charAt(x)<91)){//计算大写字母数
capCount++;
}
长度=(x+1);//检查密码长度
}//结束for循环
如果(numCount<1){//检查数字
结果=“密码中的数字不够!”;
}
如果(capCount<1){//检查大写字母
结果=“您至少需要大写字母!”;
}
如果(长度<7){//检查长度
result=“密码长度必须为7个字符”;
}
返回(结果);}
通过稍微修改已有的逻辑,基本上可以实现您想要的。无需对代码进行任何重大重构,您可以执行以下操作:
String password = "";
System.out.print("Please enter password : ");
while(!isValid(password)) {
password = in.nextLine();
if(!isValid(password)) {
System.out.println(PassCheck(password));
System.out.print("Password is not valid, try again : ");
}
}
具有所需结果的样本输出:
Please enter password : test
Password is not valid, try again : test2
Password is not valid, try again : Test1test
Please re-enter the password to confirm : Test1test
The password is valid
完整代码:
public class Password {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String password = "";
System.out.print("Please enter password : ");
while(!isValid(password)) {
password = in.nextLine();
if(!isValid(password)) {
System.out.println(PassCheck(password));
System.out.print("Password is not valid, try again : ");
}
}
System.out.print("Please re-enter the password to confirm : ");
String confirm = in.nextLine();
boolean condition;
condition = isValid(password);
while (!password.equals(confirm) || (!condition)) {
System.out.println("The password is invalid");
System.out.print("Please enter the password again : ");
password = in.nextLine();
System.out.println(PassCheck(password));
System.out.print("Please re-enter the password to confirm : ");
confirm = in.nextLine();
System.out.println(PassCheck(password));
condition = isValid(password);
}
if (isValid(password)) {
System.out.println("The password is valid");
}
}
public static boolean isValid(String password) {
Boolean atleastOneUpper = false;
Boolean atleastOneLower = false;
Boolean atleastOneDigit = false;
if (password.length() < 8) { // If its less then 8 characters, its automatically not valid
return false;
}
for (int i = 0; i < password.length(); i++) { // Lets iterate over only once. Saving time
if (Character.isUpperCase(password.charAt(i))) {
atleastOneUpper = true;
} else if (Character.isLowerCase(password.charAt(i))) {
atleastOneLower = true;
} else if (Character.isDigit(password.charAt(i))) {
atleastOneDigit = true;
}
}
return (atleastOneUpper && atleastOneLower && atleastOneDigit); // Return true IFF the password is atleast eight characters long, has atleast one upper, lower and digit
}
public static String PassCheck(String Password) {
String result = "Valid Password"; // Sets valid
int length = 0; // Stores the #
int numCount = 0; // store numbers in the password
int capCount = 0; // Variable used to store capital letters in the password
for (int x = 0; x < Password.length(); x++) {
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91)
|| (Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
} else {
result = "Password Contains Invalid Character!";//Checks that password contains only letters and numbers
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) { // Counts the number of numbers
numCount++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) { // Counts the number of capital letters
capCount++;
}
length = (x + 1); // check passwords length
} // Ends the for loop
if (numCount < 1) { // Checks numbers
result = "Not Enough Numbers in Password!";
}
if (capCount < 1) { // Checks that Capital letters
result = "You need atleast least Capital letter!";
}
if (length < 7) { // Checks length
result = "Password needs to be 7 characters long";
}
return (result);
}
}
公共类密码{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
字符串密码=”;
系统输出打印(“请输入密码:”);
而(!isValid(password)){
password=in.nextLine();
如果(!isValid(密码)){
System.out.println(PassCheck(password));
System.out.print(“密码无效,请重试:”);
}
}
System.out.print(“请重新输入密码以确认:”);
字符串confirm=in.nextLine();
布尔条件;
条件=有效(密码);
而(!password.equals(confirm)| |(!condition)){
System.out.println(“密码无效”);
System.out.print(“请再次输入密码:”);
password=in.nextLine();
System.out.println(PassCheck(password));
System.out.print(“请重新输入密码以确认:”);
confirm=in.nextLine();
System.out.println(PassCheck(password));
条件=有效(密码);
}
如果(有效(密码)){
System.out.println(“密码有效”);
}
}
公共静态布尔值isValid(字符串密码){
布尔值atleastOneUpper=false;
布尔值atleastOneLower=false;
布尔值至少一位数=false;
如果(password.length()<8){//如果少于8个字符,则其自动无效
返回false;
}
对于(inti=0;i=47&&Password.charAt(x)=64&&Password.charAt(x)=97&&Password.charAt(x)47&&Password.charAt(x)<58)){//
numCount++;
}
String password = "";
System.out.print("Please enter password : ");
while(!isValid(password)) {
password = in.nextLine();
if(!isValid(password)) {
System.out.println(PassCheck(password));
System.out.print("Password is not valid, try again : ");
}
}
Please enter password : test
Password is not valid, try again : test2
Password is not valid, try again : Test1test
Please re-enter the password to confirm : Test1test
The password is valid
public class Password {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String password = "";
System.out.print("Please enter password : ");
while(!isValid(password)) {
password = in.nextLine();
if(!isValid(password)) {
System.out.println(PassCheck(password));
System.out.print("Password is not valid, try again : ");
}
}
System.out.print("Please re-enter the password to confirm : ");
String confirm = in.nextLine();
boolean condition;
condition = isValid(password);
while (!password.equals(confirm) || (!condition)) {
System.out.println("The password is invalid");
System.out.print("Please enter the password again : ");
password = in.nextLine();
System.out.println(PassCheck(password));
System.out.print("Please re-enter the password to confirm : ");
confirm = in.nextLine();
System.out.println(PassCheck(password));
condition = isValid(password);
}
if (isValid(password)) {
System.out.println("The password is valid");
}
}
public static boolean isValid(String password) {
Boolean atleastOneUpper = false;
Boolean atleastOneLower = false;
Boolean atleastOneDigit = false;
if (password.length() < 8) { // If its less then 8 characters, its automatically not valid
return false;
}
for (int i = 0; i < password.length(); i++) { // Lets iterate over only once. Saving time
if (Character.isUpperCase(password.charAt(i))) {
atleastOneUpper = true;
} else if (Character.isLowerCase(password.charAt(i))) {
atleastOneLower = true;
} else if (Character.isDigit(password.charAt(i))) {
atleastOneDigit = true;
}
}
return (atleastOneUpper && atleastOneLower && atleastOneDigit); // Return true IFF the password is atleast eight characters long, has atleast one upper, lower and digit
}
public static String PassCheck(String Password) {
String result = "Valid Password"; // Sets valid
int length = 0; // Stores the #
int numCount = 0; // store numbers in the password
int capCount = 0; // Variable used to store capital letters in the password
for (int x = 0; x < Password.length(); x++) {
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91)
|| (Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
} else {
result = "Password Contains Invalid Character!";//Checks that password contains only letters and numbers
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) { // Counts the number of numbers
numCount++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) { // Counts the number of capital letters
capCount++;
}
length = (x + 1); // check passwords length
} // Ends the for loop
if (numCount < 1) { // Checks numbers
result = "Not Enough Numbers in Password!";
}
if (capCount < 1) { // Checks that Capital letters
result = "You need atleast least Capital letter!";
}
if (length < 7) { // Checks length
result = "Password needs to be 7 characters long";
}
return (result);
}
}