【问题标题】:Adding element to array and using that array again将元素添加到数组并再次使用该数组
【发布时间】:2019-05-03 15:59:12
【问题描述】:

对于一门课程,我必须创建一个程序,要求用户输入密码,如果他们输入的密码尚未列出,请将其添加到列表中,并允许他们再次输入密码并正确。我尝试编写一种方法来复制数组并添加新密码,但它不起作用。有人能告诉我这里哪里出错了吗?哦,我必须使用数组而不是数组列表:(

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    String pin;

    do {
        System.out.print("Please enter the password: ");
        pin = console.nextLine();
        checkPassword(pin);
    } while (!checkPassword(pin));



}

public static boolean checkPassword (String pin) {
    String[] passwords = {"9999", "coastal", "1"};

    if (Arrays.stream(passwords).anyMatch(pin::equals)) {
        return true;
    } else {
        int count = 0;
        String[] newWord = new String[passwords.length + 1];
        for(int i=0; i < passwords.length; i++){
             newWord[i]= passwords[i];
             count++;
        }
        newWord[count] = pin;

        passwords = newWord;
        System.out.print("Password was not in array. It has now been added. Try logging in again.");
        return false;
    }
}

【问题讨论】:

    标签: java arrays loops passwords


    【解决方案1】:

    你的第一个错误是你使用局部变量作为密码,所以当你调用方法时它总是被初始化。

    其次,你要检查两次密码:第一次检查,然后循环条件检查。

    因此建议您使用一个字段而不是局部变量作为密码,并且只执行一次检查,然后程序将正常工作。

    更新代码:

    public class Test {
        private static String[] passwords = {"9999", "coastal", "1"};
    
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
            String pin;
    
            boolean valid = false;
            do {
                System.out.print("Please enter the password: ");
                pin = console.nextLine();
                valid = checkPassword(pin);
            } while (!valid);
    
    
        }
    
        public static boolean checkPassword(String pin) {
            if (Arrays.stream(passwords).anyMatch(pin::equals)) {
                return true;
            } else {
                int count = 0;
                String[] newWord = new String[passwords.length + 1];
                for (int i = 0; i < passwords.length; i++) {
                    newWord[i] = passwords[i];
                    count++;
                }
                newWord[count] = pin;
    
                passwords = newWord;
                System.out.print("Password was not in array. It has now been added. Try logging in again.");
                return false;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      根据我对您的问题的理解,您有一个密码列表,您尝试做的是要求用户插入密码,然后重新插入,以验证重新插入的密码是否匹配第一个。

      为此,您必须提示用户输入并将收到的密码添加到密码列表中。之后再次提示用户输入相同的密码并检查该密码是否存在于密码列表中。如果插入正确,则该密码应该在用户第一次添加后就存在。

      你正在做的这个实现并不是最好的。尝试使用字典将密码与用户名相关联。您还可以请求 2 个输入并比较它们以验证它们是否相同,然后将密码添加到字典中。

      希望对你有帮助。

      【讨论】:

        【解决方案3】:

        您面临的问题与变量范围有关。每当当前方法执行结束时,其变量将超出范围并有资格进行垃圾回收。你可以说passwords是一个局部变量。

        现在,很难在方法中保存数组的更新版本,它将被丢弃并且对后续调用不可见。

        public static boolean checkPassword (String pin) {
            String[] passwords = {"9999", "coastal", "1"};
            ...
        } // here, passwords goes out of scope
        

        要修复您的错误,您必须将其全局存储或将其添加为方法参数。

        // global
        private static String[] passwords = {"9999", "coastal", "1"};
        
        // method parameter
        public static boolean checkPassword (String pin, String[] passwords) {
            ...
        }
        

        【讨论】:

          猜你喜欢
          • 2015-05-11
          • 1970-01-01
          • 2013-10-22
          • 2012-01-23
          • 2016-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-04
          相关资源
          最近更新 更多