==
运算符或.equals
方法似乎无效。
我想要的是将“对象引用”与“字符串值”进行比较。
public static void main(String[] args) {
String sec1[]={"IAD","Bwi","IND"};
String sec2[]={"1","2","3","4"};
int leng1 = sec1.length;
int leng2 = sec2.length;
for (int counter = 0; counter < 5; counter ++) {
int rand1 = (int)(Math.random()*leng1);
int rand2 = (int)(Math.random()*leng2);
String user = sec1[rand1] + " " + sec2[rand2] + " ";
System.out.println(user);
if (user == "Bwi 2") { *// to see if user = Bwi 2 .(here is the problem)*
System.out.println("It's");
break;
} else {
System.out.println("It's not");
}
}
}
我想要的是,如果“ user ==” Bwi 2“然后停在那里。