嘗試實作默認建構式和一些基本功能。雖然這個程式背后的邏輯很清楚,但我不知道為什么每次輸出都是錯誤的。為了避免任何型別的問題,我明確地進行了型別轉換,但似乎錯誤仍未解決。
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.");
}
}
}
uj5u.com熱心網友回復:
==-Operator 檢查 Java 中的參考,因此只有當您的物件相同時才有效,而它們不是。您需要使用 equals() 方法來檢查內容是否相同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/372535.html
