【发布时间】: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