"不兼容的操作数类型int和字符串"

Dan*_*den 0 java string int if-statement

我刚开始学习代码(特别是Java),我正在测试一个密码系统,当你输入密码时,它变成变量"password",它会检查它是否等于password2,即实际密码.这是代码:

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."); 
            }   



    }   
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试检查嵌套的if语句是否输入了匹配密码2,"使命召唤"的密码; 但问题是它不适用于字符串.这个问题的标题是出现的错误.有人可以帮帮我吗?

Joa*_*uer 9

我尝试给出一个提示,而不是提供完整的答案:

检查的数据类型passwordpassword2.他们为什么不同?