-5

我做了一个申请,我把DAY_OF_MONTH条件放在(如果)之后像这样

if (cal.get(Calendar.DAY_OF_MONTH) == 9) {
        Intent intent1 = new Intent(MainActivity.this, TextActivity.class);
        intent1.putExtra("key", getResources().getString(R.string.s_monday_txt));
        startActivity(intent1);
        finish();
    } else if (cal.get(Calendar.DAY_OF_MONTH) == 10) {
        Intent intent2 = new Intent(MainActivity.this, TextActivity.class);
        intent2.putExtra("key", getResources().getString(R.string.s_tuesday_txt));
        startActivity(intent2);
        finish();
    }

在另一个地方,我像这样在(如果)之后放置了 MONTH 条件

    if (cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) {
        Intent intent1 = new Intent(MainActivity.this, TextActivity.class);
        intent1.putExtra("key", getResources().getString(R.string.s_september_txt));
        startActivity(intent1);
        finish();
    } else if (cal.get(Calendar.MONTH) == Calendar.AUGUST) {
        Intent intent2 = new Intent(MainActivity.this, TextActivity.class);
        intent2.putExtra("key", getResources().getString(R.string.s_august_txt));
        startActivity(intent2);
        finish();
    }

这对我来说很好,但问题是如何像这样同时检查这两个条件

if (cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) + (cal.get(Calendar.DAY_OF_MONTH) == 9) {
        Intent intent1 = new Intent(MainActivity.this, TextActivity.class);
        intent1.putExtra("key", getResources().getString(R.string.s_september_txt));
        startActivity(intent1);
        finish();
    } else if (cal.get(Calendar.MONTH) == Calendar.AUGUST)  + (cal.get(Calendar.DAY_OF_MONTH) == 10) {
        Intent intent2 = new Intent(MainActivity.this, TextActivity.class);
        intent2.putExtra("key", getResources().getString(R.string.s_august_txt));
        startActivity(intent2);
        finish();
    }

我试过 (+) , (and), (||), (,) 所有这些都没有任何帮助???

4

3 回答 3

0

In java, the + sign is an addition. If you want the "and" meaning, use the && sign.

&& - and

|| - or

so if you want to check if both conditions inside your if statement applies then use it like this:

if (value == 1 && value2 == 1){
 // do something if value is equal to 1 _AND_ value2 equals to 1
}
于 2013-09-12T13:03:20.623 回答
0

将其更改为&&for||for or

cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) + (cal.get(Calendar.DAY_OF_MONTH) == 9

将其更改为:

cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) && (cal.get(Calendar.DAY_OF_MONTH) == 9
于 2013-09-12T13:05:36.300 回答
0

下面是解释什么&&||意味着什么。根据您的要求使用运算符

if ((cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) && (cal.get(Calendar.DAY_OF_MONTH) == 9)) {
 // it is  september and day of month is 9
}


if ((cal.get(Calendar.MONTH) == Calendar.SEPTEMBER) || (cal.get(Calendar.DAY_OF_MONTH) == 9)) {
 // either september or 9
}

参考链接

于 2013-09-12T13:07:10.620 回答