我剛開始學習代碼(特別是Java),並且我測試了一個密碼系統,當你輸入密碼時它變成變量「password」並且它檢查它是否它等於password2,實際的密碼。下面的代碼:「不兼容的操作數類型int和字符串」
import java.util.Scanner;
public class LogicalOperators {
public static void main(String args[]){
Scanner test = new Scanner(System.in);
int age;
int password;
String password2;
password2 = "Call of Duty";
System.out.println("Please enter your age:");
age = test.nextInt();
if (age >=18) {
System.out.println("You are old enough.");
System.out.println("Please enter the password:");
password = test.nextInt();
if (password == password2) {
System.out.println("Welcome back!");
}else{
System.out.println("The password you typed was incorrect.");
}
}else{
System.out.println("You are too young.");
}
}
}
我想在嵌套的if語句是否我匹配的密碼2鍵入的密碼,「使命召喚」,以檢查;但問題是它不適用於字符串。這個問題的標題是出現的錯誤。有人可以幫幫我嗎?
這個任務沒有關閉(該術語在SO上有非常具體的含義)。如果您的問題已得到解答,只需接受最能幫助您的答案即可。 –
要檢查兩個字符串是否相同,您必須使用'.equals'方法而不是'=='來檢查引用。還要將密碼類型更改爲字符串。 – Logan