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

how can i make a username and password test method that doesnt overlap?

我需要创建一个 class 如果用户输入用户名 "alex" 密码 "mightyducks" 或用户名 "emily" 密码 "cat", 他们登录系统 下面是我的代码:

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" 时我想要的输出是 "You are now logged into the system!" 但我的实际输出是: "You are now logged into the system!" "Your username or password was invalid!" 我该如何解决这个问题?

因为一旦您在第一个 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 部分是如何工作的。

不需要做更多的 if else 块。因为 Java 提供 else if。以下代码可以为您工作。

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!");
       }
    }
}

您可以尝试使用 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