2011-08-13 54 views
1

我想在如果else语句来测试上午或下午..如何测试上午或下午?

if(am){ 
//Do something 
else{ 

//Do something else 

我用尽

int am = cld.get(Calendar.AM_PM); 

但如果其他

不会把它作为一个参数测试。也许是因为它不是布尔值。

我该如何去测试呢?

回答

2

你是正确的if-else不会接受它,因为它不是布尔值。 Calendar.AM_PM只能保存值01。像C这样的语言会接受0或1作为布尔值; Java不会。

你想要做更多的东西是这样的:

int am = cld.get(Calendar.AM_PM); 
if (am == 0) { 
    // Do whatever for the AM 
} else { 
    // Do whatever because it must be PM 
} 
0

当然,你的if子句不能接受整数,因为它是。你需要一些东西(比较可能)来获取布尔值。

if(am > 0) 
    { 
     //its PM 
    else { //its AM } 
+0

whats the 0 mean?它与我的比较是什么? –

+0

http://developer.android.com/reference/java/util/Calendar.html#AM 这就是为什么0,AM常数值为0。 –

0

Calendar.AM_PM是一个int。要在if语句中对其进行评估,请将其转换为布尔值:

if((bool)am) { 
    //It's AM 
} else { 
    //It's PM 
}