比较密码字符串中的字符



我有问题。我得到了这个练习,但我不知道如何在代码中解决最后三个解决方案。我需要打印失败,但我不能得到,因为我只得到了OK,我就是这样。

我需要比较两个密码,一个是String密码,另一个是StringcheckPassword。该程序将分别比较每个字母或数字,如果相同,则应打印OK,因此,如果将密码123与123进行比较,则应写入OK OK PASS,如果将123与124进行比较,那么应写入OK OKFAIL,如果将123-124进行比较,然后写入OK OK FAIL,如果将123-1234进行比较,那幺应写入OK OK FAIL,并且如果将123-81234进行比较则应抛出FAIL。

package NewPackage;
/*
password = 123
checkPassword(123) -> OK OK PASS
checkPassword(124) -> OK OK FAIL
checkPassword(12) -> OK OK FAIL
checkPassword(1234) -> OK OK OK FAIL
checkPassword(81234) -> FAIL
*/
public class Tworzenie_tablicy_char {

public static void main(String[] args) {
String password = "123";
String checkPassword = "12";
char[] charPassword = new char[password.length()];
char[] charCheckPassword = new char[checkPassword.length()];
for (int i = 0; i < password.length(); i++) {
charPassword[i] = password.charAt(i);
//System.out.println(password.charAt(i));
}
for (int i = 0; i < checkPassword.length(); i++) {
charCheckPassword[i] = checkPassword.charAt(i);
//System.out.println(checkPassword.charAt(i));
}
int minLength = Math.min(charPassword.length, charCheckPassword.length);
for (int i = 0; i < minLength; i++) {
boolean correctly = charPassword[i] == charCheckPassword[i];
if (correctly == true) {
System.out.println("OK");
} else {
System.out.println("FAIL");
}
}
}
}

有什么建议吗??

不需要转换为char字符串,可以使用charAt获取字符。取最小长度,检查同一索引处的两个字符是否相等。如果一个字符不相等,则退出循环,使失败标志为true,然后根据失败标志显示fail或PASS。

public class Tworzenie_tablicy_char {
public static void main(String[] args) {
String password = "123";
String checkPassword = "124";
boolean fail = false;
int minLength = Math.min(password.length(), checkPassword.length());

for (int i = 0; i < minLength; i++) {
if (password.charAt(i) == checkPassword.charAt(i))
System.out.println("OK");
else{
fail = true;
break;
}
}

if(password.length() == checkPassword.length() && !fail)
System.out.println("PASS");
else
System.out.println("FAIL");

}
}

输出

java -cp /tmp/Jk7ZebI9iH Tworzenie_tablicy_char
OK
OK
FAIL

要比较任何方法,请使用

method.equals(method you want to compare);

如果我不明白这个问题,请告诉我

我得到了这个解决方案,但我认为它是正确的,我可以得到更好、干净的解决方案。:/

package NewPackage;
/*
password = 123
checkPassword(123) -> OK OK PASS
checkPassword(124) -> OK OK FAIL
checkPassword(12) -> OK OK FAIL
checkPassword(1234) -> OK OK OK FAIL
checkPassword(81234) -> FAIL
*/
public class Tworzenie_tablicy_char {

public static void main(String[] args) {
String password = "123";
String checkPassword = "12";
char[] charPassword = new char[password.length()];
char[] charCheckPassword = new char[checkPassword.length()];
for (int i = 0; i < password.length(); i++) {
charPassword[i] = password.charAt(i);
//System.out.println(password.charAt(i));
}
for (int i = 0; i < checkPassword.length(); i++) {
charCheckPassword[i] = checkPassword.charAt(i);
//System.out.println(checkPassword.charAt(i));
}
int minLength = Math.min(charPassword.length, charCheckPassword.length);
if (charPassword.length < charCheckPassword.length)
{
System.out.println("FAIL");
}else {
for (int i = 0; i < minLength; i++) {
boolean correctly = charPassword[i] == charCheckPassword[i];
if (correctly == true) {
System.out.println("OK");
} else {
System.out.println("FAIL");
}
}
if (charPassword.length != charCheckPassword.length) {
System.out.println("FAIL");
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新