【问题标题】:How to use Methods to solve: Locking account after three times?怎么用 解决方法: 3次锁定账号?
【发布时间】:2019-10-15 03:47:25
【问题描述】:

我正在尝试创建一个使用方法来锁定/解锁帐户的程序。 这是我的程序,但是当我尝试运行它时,它说我的方法中用于询问用户名和密码的参数不能为空。

我是一个初学者,刚刚学会了如何使用方法,所以如果有人可以帮助解释这个程序的错误是什么,那就太好了。

我还尝试创建一种在使用循环尝试三次后锁定帐户的方法,但我坚持这样做。请给点建议,谢谢!

//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

【问题讨论】:

  • ask 方法采用一个 String 参数,但您在没有参数的情况下调用它。这行不通。在调用方法时传递参数或摆脱它。在 ask 方法应该做什么的上下文中,第二个选项似乎更合理。
  • 感谢您的回答!能否详细解释一下“调用方法时传递参数”?

标签: java loops methods


【解决方案1】:

public static String ask(String password) 中删除String password

当您在 String pass_word = ask(); 调用 ask 方法时,您没有在 ask() 中传递任何参数,但您的方法声明中有一个参数 String password。由于您没有传递参数,因此它会给您该错误。

这就是你的ask() 方法应该是这样的:

public static String ask(){
    Scanner input = new Scanner (System.in);

    System.out.println("Enter Password: ");
    password = input.nextLine();

    return password;
}

就三个尝试而言,请尝试运行它。我在这里也做了上面提到的更改,所以只需运行它,看看它是否有效:

import java.util.Scanner;

public class Lock {
    public static void main(String [] args){

        String user_name = question();
        String pass_word = ask();

        int count = 0;

        while (count < 4) {
            int result = testing(user_name, pass_word);

            if (result == 0) {
                System.out.println("Welcome");
            }
            count++;
        }

        if (count > 3) {
            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();

        input.close();

        return username;
    }

    //3. method for asking password
    public static String ask(){
        Scanner input = new Scanner (System.in);

        System.out.println("Enter Password: ");
        String password = input.nextLine();

        input.close();

        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)))
            result = 1;

        return result; //return statment

    }
}

【讨论】:

  • 非常感谢!这说明了很多。我试图为这个锁定帐户创建一个方法,但我不确定在这种情况下返回语句应该是什么。 public static String test (String password){ for (int i = 0; i
  • 不要给测试方法一个返回类型的字符串,而是让它成为一个布尔值:public static boolean test (String password)。初始化变量boolean isLocked = false。进行此更改 if (! (password.equals("12345"))){ isLocked = true; System.out.println("Try again"); } else { isLocked = false; break; } 并在方法结束时返回 isLocked。您将在方法调用期间收到 truefalse 值并运行 if 语句以打印相关消息
猜你喜欢
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 2021-09-06
  • 2011-07-31
  • 1970-01-01
  • 2022-06-16
  • 2023-02-08
  • 1970-01-01
相关资源
最近更新 更多