我對 Java 很陌生,昨天才開始學習,這是我的班級練習,代碼必須與此類似。我正在嘗試用 Java 創建一個簡單的登錄系統。這是我收到的錯誤: Error:(18, 11) java: invalid start of type我有一個朋友有類似的代碼,它對他有用,但他無法具體說明為什么我的代碼不起作用。我嘗試刪除大括號。我嘗試制作不同的 if 語句,并嘗試在 read.nextLine(); 周圍加上圓括號。System.out.print("Type your username: "); String username = reader.nextLine(); System.out.print("Type your password: "); String password = reader.nextLine(); if (username = "Alex" && password = "mightyducks") { System.out.print("You are now logged into the system!"); } else if (username = "Emily" && password = "cat") System.out.print("You are now logged into the system!"); } else { System.out.print("Your username or password was invalid!"); }我只是想讓程序顯示“您現在已登錄系統!” 當為 Alex 或 Emily 提供正確的密碼時,并以其他方式說明:“您的用戶名或密碼無效!”。
2 回答

德瑪西亞99
TA貢獻1770條經驗 獲得超3個贊
該行末尾缺少一個左大括號:
} else if (username = "Emily" && password = "cat") {
而且錯誤還比較多。用于.equals
比較字符串。=
是一個在這里完全錯誤的作業。

qq_笑_17
TA貢獻1818條經驗 獲得超7個贊
您缺少第一個左大括號else if
另外,您還必須使用==來比較而不是=。對于字符串和其他類型的對象,您應該使用方法equals來==比較引用而不是內容。
if (username.equals( "Alex") && password.equals("mightyducks")) {
System.out.print("You are now logged into the system!");
} else if (username.equals( "Emily") && password.equals("cat")){
System.out.print("You are now logged into the system!");
} else {
System.out.print("Your username or password was invalid!");
}
添加回答
舉報
0/150
提交
取消