java如何使用方法解决:锁定帐户三次后?
我正在尝试创建一个使用方法锁定/解锁帐户的程序。 这是我的程序,但当我试着运行它时,它说我询问用户名和密码的方法中的参数不能为空
我是一个初学者,刚刚学会了如何使用方法,所以如果有人能帮助解释这个程序的错误,那就太好了
我还试图创建一个方法来锁定帐户后,三次尝试使用循环,但我被卡住了。请给我一些建议,谢谢
//Lock account after n=3 using methods
//using Methods created
import java.util.Scanner;
public class Lock_Methods{
public static void main(String [] args){
String user_name = question();
String pass_word = ask();
int result = testing(user_name, pass_word);
if (result == 0){
System.out.println("Welcome");
}
else
System.out.println("Locked");
} //end of main
//2. method for asking username
public static String question(){
Scanner input = new Scanner (System.in);
System.out.println("Enter username: ");
String username = input.nextLine();
return username;
}
//3. method for asking password
public static String ask(String password){
Scanner input = new Scanner (System.in);
System.out.println("Enter Password: ");
password = input.nextLine();
return password;
}
//4. method for testing whether username and password are true
public static int testing(String inputname, String inputpw){
int result = 0;
String username = "edu";
String password = "12345";
if (!(username.equals(inputname)) || ! (password.equals(inputpw)))
return 1;
else
return 0;
return result; //return statment
}
} //end of class
# 1 楼答案
从
public static String ask(String password)
中删除String password
当您在
String pass_word = ask();
调用ask方法时,您没有在ask()
中传递任何参数,但是您的方法声明中有一个参数String password
。因为您没有传递参数,所以它会给您该错误这就是你的
ask()
方法应该是这样的:就三次尝试而言,试着运行这个。我在这里也做了上面提到的更改,所以只需运行这个,看看它是否有效: