如何制作不重叠的用户名和密码测试方法?

时间:2015-01-20 02:45:42

标签: java string

我需要创建一个类,如果用户使用密码“mightyducks”输入用户名“alex”或使用密码“cat”输入用户名“emily”,他们将登录到系统 这是我的代码:

import java.util.Scanner;

public class Usernames {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.println("Type your username:");
        String user = reader.nextLine();
        System.out.println("Type your password:");
        String pass = reader.nextLine();

        if (pass.equals("mightyducks")&& user.equals("alex")) {
            System.out.println("You are now logged into the system!"); 
        } else {
            System.out.println("Your username or password was invalid!");
        }   
        if (user.equals("emily") && pass.equals("cat")) {
            System.out.println("You are now logged into the system!");
        } else {
         System.out.println("Your username or password was invalid!");
        }
    }
}

当我输入“alex + mightyducks”或“emily + cat”时,我想要的输出是“你现在已登录系统了!” 但我的实际输出是: “你现在登录了系统!” “您的用户名或密码无效!” 我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

因为一旦您在第一个if语句中对用户进行了授权,您就会通过并测试第二个if语句。类似地,如果您输入无效凭据,则会收到两次错误消息(一次用于第一个if / else块,另一次用于第二个if / else块)。

尝试简化一下:

if (pass.equals("mightyducks") && user.equals("alex")) {
    System.out.println("You are now logged into the system!"); 
} else if (user.equals("emily") && pass.equals("cat")) {
    System.out.println("You are now logged into the system!");
} else {
    System.out.println("Your username or password was invalid!");
}

通过折叠前两个测试,有一些方法可以让它更简单一点,但我想用这种方式来说明if / else部分是如何工作的。

答案 1 :(得分:0)

没有必要制作更多if if block。如果Java提供了其他的话。下面的代码可以为你工作。

import java.util.Scanner;

public class Match_Name_Pass 
{
    public static void main(String[] args) 
     {
       Scanner scn = new Scanner(System.in);
       System.out.println("Enter the user name : ");
       String username = scn.next();
       System.out.println("Enter the password");
       String password = scn.next();

       if(username.equals("alex")  && password.equals("mightyducks") )
        {
        System.out.println("You are now logged into the system!");
        }
       else if(username.equals("emily") && password.equals("cat"))
        {
        System.out.println("You are now logged into the system!");
        }
       else
       {
        System.out.println("Your username or password was invalid!");
       }
    }
}

答案 2 :(得分:0)

您可以尝试使用Map来取消multiple if-else块:

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

class Ideone {

  public static void main(String[] args) {
    Map <String, String> map = new HashMap <String, String> ();
    map.put("alex", "mightyducks");
    map.put("emily", "cat");
    Scanner reader = new Scanner(System.in);
    System.out.println("Type your username:");
    String user = reader.nextLine();
    System.out.println("Type your password:");
    String pass = reader.nextLine();
    reader.close();
    if (null != map.get(user) && map.get(user).equals(pass)) {
      System.out.println("You are now logged into the system!");
    } else {
      System.out.println("Your username or password was invalid!");
    }
  }
}

示例code works here