JAVA programming 問題

2008-11-26 8:13 pm
public class Main {

public static void main(String[] args){

String date = "01/01/2001";

String first = date.substring(2, 3);
String second = date.substring(5, 6);

System.out.println(first+second);

if (first=="/")
System.out.println("first ok");
if (second=="/")
System.out.print("second ok");
}

}

========================
以上未能print out 最後的"first ok"以及"second ok"
但System.out.println(first+second)中print out的確實是兩個"/"
請問問題出在哪?

回答 (1)

2008-11-26 8:26 pm
✔ 最佳答案
if (first.equals("/"))
System.out.println("first ok");
if (second.equals("/"))
System.out.print("second ok");

String (字串)要用 equals
char, int, long, double, byte, float 用 ==


收錄日期: 2021-04-23 21:13:42
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081126000051KK00507

檢視 Wayback Machine 備份