为什么即使输入正确的值
,它也总是登录失败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");
}
}
}
答案 0 :(得分:2)
尝试使用equals
比较字符串if (usrname.equals(usrnameInput) && usrpass.equals(usrpassInput)) {
System.out.println("Login Successful");
} else {
System.out.println("Lofin failed");
}