【发布时间】:2011-10-13 05:58:24
【问题描述】:
我刚开始学习代码(特别是 Java),我正在测试一个密码系统,当你输入密码时,它会变成变量“密码”,它会检查它是否等于密码 2,即实际密码。代码如下:
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方法而不是检查参考的==。还将密码类型更改为字符串。
标签: java string if-statement int