多次检查并显式进行类型转换以避免错误,但该程序仍然显示错误的结果。该怎么办?
Checked multiple times and also typecasted explicitly to avoid errors but still this program showing wrong results. What to do?
正在尝试实现默认构造函数和一些基本功能。 虽然这个程序背后的逻辑很清楚,但我不知道为什么每次输出都是错误的。为了避免出现任何类型的问题,我明确输入了类型,但似乎错误仍未解决。
import java.util.*;
class Facebook {
Facebook() {
System.out.printf("Welcome to Facebook.\n");
}
boolean Login(String uName, String pWord) {
if(uName == "knownassurajit" && pWord == "password")
return true;
else
return false;
}
public static void main(String[] args) {
Facebook user = new Facebook();
Scanner scanner = new Scanner(System.in);
System.out.printf("Username: ");
String username = scanner.next();
System.out.printf("Password: ");
String password = scanner.next();
//System.out.println(username.length() + " " + password.length());
if(user.Login((String) username, (String) password)) {
System.out.println("Successfully logged in.");
}
else {
System.out.println("Username or Password incorrect.");
}
}
}
此代码将与 equals() 函数完美配合。
import java.util.*;
class Facebook {
Facebook() {
System.out.printf("Welcome to Facebook.\n");
}
boolean Login(String uName, String pWord) {
if(uName.equals("user") && (pWord.equals("p@$$w0rd")))
return true;
else
return false;
}
public static void main(String[] args) {
Facebook user = new Facebook();
Scanner scanner = new Scanner(System.in);
System.out.printf("Username: ");
String username = scanner.next();
System.out.printf("Password: ");
String password = scanner.next();
if(user.Login((String) username, (String) password)) {
System.out.println("Successfully logged in.");
}
else {
System.out.println("Username or Password incorrect.");
}
}
}
==-运算符检查 Java 中的引用,因此只有当您的对象相同时才有效,而它们不是。需要使用equals()方法判断内容是否相同
正在尝试实现默认构造函数和一些基本功能。 虽然这个程序背后的逻辑很清楚,但我不知道为什么每次输出都是错误的。为了避免出现任何类型的问题,我明确输入了类型,但似乎错误仍未解决。
import java.util.*;
class Facebook {
Facebook() {
System.out.printf("Welcome to Facebook.\n");
}
boolean Login(String uName, String pWord) {
if(uName == "knownassurajit" && pWord == "password")
return true;
else
return false;
}
public static void main(String[] args) {
Facebook user = new Facebook();
Scanner scanner = new Scanner(System.in);
System.out.printf("Username: ");
String username = scanner.next();
System.out.printf("Password: ");
String password = scanner.next();
//System.out.println(username.length() + " " + password.length());
if(user.Login((String) username, (String) password)) {
System.out.println("Successfully logged in.");
}
else {
System.out.println("Username or Password incorrect.");
}
}
}
此代码将与 equals() 函数完美配合。
import java.util.*;
class Facebook {
Facebook() {
System.out.printf("Welcome to Facebook.\n");
}
boolean Login(String uName, String pWord) {
if(uName.equals("user") && (pWord.equals("p@$$w0rd")))
return true;
else
return false;
}
public static void main(String[] args) {
Facebook user = new Facebook();
Scanner scanner = new Scanner(System.in);
System.out.printf("Username: ");
String username = scanner.next();
System.out.printf("Password: ");
String password = scanner.next();
if(user.Login((String) username, (String) password)) {
System.out.println("Successfully logged in.");
}
else {
System.out.println("Username or Password incorrect.");
}
}
}
==-运算符检查 Java 中的引用,因此只有当您的对象相同时才有效,而它们不是。需要使用equals()方法判断内容是否相同