当前位置: 首页 > 知识库问答 >
问题:

比较密码字符串中的字符

常雅达
2023-03-14

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

html prettyprint-override">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");
            }
        }
    }
}

有什么建议吗??

共有1个答案

贺博厚
2023-03-14

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

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");
            }
        }
    }
}
 类似资料:
  • 问题内容: 我听说散列(即将字符串或对象转换为数字)用于字符串等,因为比较数字比字符串更容易。如果为真,这是什么原因? 问题答案: 不一定是这种情况,但大多数时候可能是这样。 请考虑以下情况: 我想比较字符串“ apples”和“ oranges”。如果我只想确定“ apples” ==“ oranges”,我只需要比较每个字符串的第一个字符:’a’!=’o’=>“ apples”!=“ oran

  • 问题内容: 给我输出: 我不太了解第一行,为什么它会给我带来错误? 如果我在字符串2上添加空格,则此行为false,但如果不添加空格,则为true: 为什么第1行给我假,但第6行为真,但是如果我在string2上添加空格则为假。 谢谢=) 问题答案: 那里发生了几件事。 当您将两个字符串声明为相等的“ Hi”时,java将对其进行优化以引用相同的字符串对象(因此不必将相同的字符串存储两次)。 “

  • 问题内容: “按字典顺序比较两个字符串”是什么意思? 问题答案: 从@Bozho和@aioobe的答案出发,字典比较类似于在字典中可能会发现的顺序。 Java String类提供了该方法以便按字典顺序比较String。这样使用。 此方法的返回int值可以解释如下: 返回<0,则调用该方法的String按字典顺序排在首位(在字典中排在首位) 返回== 0,则这两个字符串在字典上等效 返回> 0,然后

  • 问题内容: 我需要查询表中给定字符串的值。该表区分大小写,但我想在比较中执行ToLower()。 假设我有一个包含以下数据的类表。 我的查询应该是这样的 这是进行比较的最佳方法吗? 更新 我无法控制数据库或数据。我是只读用户。 问题答案: 不; 最好改善数据:创建一个数字ID,以表示这些看似毫无意义的类变体(可能还有一个相关的查找表来获取ID)。使用where子句中的ID列,您应该找到一个索引数字

  • 主要内容:equals() 方法,equalsIgnoreCase() 方法,equals()与==的比较,compareTo() 方法字符串比较是常见的操作,包括比较相等、比较大小、比较前缀和后缀串等。 在 Java 中,比较字符串的常用方法有 3 个:equals() 方法、equalsIgnoreCase() 方法、 compareTo() 方法。下面详细介绍这 3 个方法的使用。 equals() 方法 equals() 方法将逐个地比较两个字符串的每个字符是否相同。如果两个字符串具有相

  • 我已经声明了一个实现可比较接口和compareTo方法的类,使用employee ID比较两个员工。创建的类对象插入数组列表。现在,当我使用collections.sort(arrayList对象)时,它工作得很好。我对collective和comparator接口之间的比较有何不同感到困惑。我想知道如何在纯粹由数字组成的employee id字符串和其他字符串employee id之间进行比较,