看板 java 關於我們 聯絡資訊
public class test { public static void main(String[] args){ String s1 ="a + b"; String[] s2 =s1.split(" "); for(int i=0 ;i<=s2.length-1 ;i++ ){ System.out.print(s2[i]+" "); if(s2[i]=="+" ) System.out.println("1"); else System.out.println("2"); } } } console: a 2 + 2 b 2 有問題的在if那行 當i=1,為什麼s2[i]="+" 是false? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.168.23.84
PsMonkey:字串要判斷是不是相等不是用 ==,而是用 equals() 10/11 22:47
PsMonkey:應該說字串是 object,而兩個 object 的 == 是判斷 ref 10/11 22:48
orze04:感謝 太久沒用都忘記了 10/11 23:08
henry4343:原來兩個object的==是判斷ref(筆記....) 10/12 00:36
pugboy:也可以參考此篇文http://ppt.cc/g7kO 10/12 04:27