即使输入正确的值,为什么它总是登录失败

时间:2016-06-13 01:13:06

标签: java login

为什么即使输入正确的值

,它也总是登录失败
import java.util.Scanner;

public class Main 
{
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub


    String usrname = "aaa";
    String usrpass = "aaa";

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter your username :");
    String usrnameInput = sc.nextLine();

    System.out.println("Enter your password :");
    String usrpassInput = sc.nextLine();


    if (usrname  == usrnameInput & usrpass == usrpassInput) {
        System.out.println("Login Successful");
    } else {
        System.out.println("Lofin failed");
    }



}

}

1 个答案:

答案 0 :(得分:2)

尝试使用equals

比较字符串
if (usrname.equals(usrnameInput)  && usrpass.equals(usrpassInput)) {
    System.out.println("Login Successful");
} else {
    System.out.println("Lofin failed");
}