為什么要用equal?不用equal也可以實現?。?/h1>
public class HelloWorld {
? ? public static void main(String[] args) {
int score = 94;
String sex = "女";
? ? ? ? if(score>80){
? ? ? ? ? ? System.out.println("進入決賽");
? ? ? ? ? ? if(sex=="女"){
? ? ? ? ? ? ? ? System.out.println("您在女子組");
? ? ? ? ? ? } else{
? ? ? ? ? ? ? ? System.out.println("您在男子組");
? ? ? ? ? ? }
? ? ? ? }
public class HelloWorld {
? ? public static void main(String[] args) {
int score = 94;
String sex = "女";
? ? ? ? if(score>80){
? ? ? ? ? ? System.out.println("進入決賽");
? ? ? ? ? ? if(sex=="女"){
? ? ? ? ? ? ? ? System.out.println("您在女子組");
? ? ? ? ? ? } else{
? ? ? ? ? ? ? ? System.out.println("您在男子組");
? ? ? ? ? ? }
? ? ? ? }
2015-02-27
感覺好難學啊
2015-02-08
==是用來判斷字符串對象的引用是否指向同一個對象的,你這里的String sex = "女",是創建了一個字符串直接量對象,然后用sex引用該對象,判斷條件里的"女"也是字符串直接量的對象,在Java中相同的字符串直接量對象只創建一次,也就是說sex引用指向的"女",與判斷條件中創建的"女"是同一個對象,所以返回true,你如果把sex的創建改成:String sex = new String("女");,那么sex == "女"返回的就是false了,就必須用equal方法了