简体   繁体   English

if else-if not working

[英]if else-if not working

I have an if else-if block as follows but it works only half-way.The if part works and so does the first if-else.我有一个 if else-if 块,如下所示,但它只能在中途工作。if 部分有效,第一个 if-else 也是如此。 But the last two if-else statements are never getting executed even in cases when they should evaluate to true.但是最后两个 if-else 语句永远不会被执行,即使在它们应该评估为真的情况下也是如此。 Can someone tell me what am I doing wrong here?有人可以告诉我我在这里做错了什么吗?

//Getting current date and time
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);                     
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

//Getting user-entered date and time values.
String[] selected_date_string = date.getText().toString().split("-");
int selected_month = Integer.parseInt(selected_date_string[0]);
int selected_day = Integer.parseInt(selected_date_string[1]);
int selected_year = Integer.parseInt(selected_date_string[2]);
String[] selected_time_string = time.getText().toString().split(":");
int selected_hour = Integer.parseInt(selected_time_string[0]);
int selected_minute = Integer.parseInt(selected_time_string[1]);

boolean has_error = false;
int error = -1;

//PROBLEM AREA
if (selected_year < year) {
    error = 1;
} else if ((selected_year == year) && (selected_month < month)) {
    error = 2;
} else if ((selected_year == year) && (selected_month == month)) {//this part doesnt work no matter what I do!
    error = 3;
} else if ((selected_year == year) && (selected_month == month) && (selected_day == day)) //this part too!
    if (selected_hour < hour) 
        error = 4;

Thanks in advance!提前致谢!

Usually we forget that MONTH in Calendar is zero based, in other words: the value for january is 0 and not 1 , as we expect..通常我们会忘记Calendar中的MONTH是从零开始的,换句话说:一月的值是0而不是1 ,正如我们所期望的那样。

You may have to decrement your selected_month ...您可能需要减少您的selected_month ...


use this line in your code:在您的代码中使用这一行:

int selected_month = Integer.parseInt(selected_date_string[0]) - 1;

You have a duplicate statement:你有一个重复的声明:

else if ((selected_year == year) && (selected_month == month))

The last one will never be evaluated because the previous one will already pick it up.最后一个永远不会被评估,因为前一个已经把它捡起来了。 I know the second one is more specific, but that won't matter.我知道第二个更具体,但这并不重要。 For example, if your years match up and your months match up, it won't matter that your days do too because the first statement that becomes true is your error = 3 else if statement.例如,如果您的年份匹配并且您的月份匹配,那么您的日子也没有关系,因为第一个变为真的语句是您的error = 3 else if 语句。

As for why neither of the bottom two statements get evaluated, I believe @Andreas_D is onto it when he mentions that the month is a zero-based value.至于为什么最后两个语句都没有得到评估,我相信@Andreas_D 在提到月份是从零开始的值时就在上面。 It may be that the statement is working, just not as you expected.可能是该语句有效,只是与您预期的不同。

Calender class in Java returns 0-11 for month, Java 中的日历 class 返回月份的 0-11,

so if current month is Jan then it returns 0所以如果当前月份是 Jan 那么它返回 0

for feb 1 as so on,对于 2 月 1 日,依此类推,

for December it returns 11对于 12 月,它返回 11

so your if condition never equals any way所以你的 if 条件永远不会等于任何方式

check it核实

comment or reply if any issue regarding it如果有任何问题,请发表评论或回复

You should put the most specific condition as the first if.您应该将最具体的条件作为第一个 if。 The last if can't execute as its condition is more specific than the 3rd one ( (selected_year == year) && (selected_month == month) ) but that first part returns true for the 3rd one before the last one get checked, so you'll never get the last if to execute even if it's true for the last part ( selected_day == day ).最后一个 if 无法执行,因为它的条件比第三个( (selected_year == year) && (selected_month == month) )更具体,但第一部分在最后一个被检查之前为第三个返回 true,所以即使最后一部分( selected_day == day )是真的,你也永远不会得到最后一个 if 执行。

Putting that condition as the first one would make sure it will get checked.将该条件作为第一个条件将确保它会被检查。

Try This.尝试这个。

int month = c.get(Calendar.MONTH)+1;

I would use a switch case construct instead.我会改用switch case构造。

As I execute this piece of code:当我执行这段代码时:

Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

result it:结果:

day = 28
month = 5
year = 2011
hour = 15
minute = 24

So maybe you are passin 06-28-2011 to test it and never got correctly compared, so if I print out error at the end with "07-11-2011" and "18:00" it print -1所以也许你在 2011 年 6 月 28 日通过测试它并没有得到正确的比较,所以如果我在最后打印出错误 "07-11-2011" 和 "18:00" 它打印 -1

When I use it with "05-11-2011" it prints 3当我将它与“05-11-2011”一起使用时,它会打印 3

my suggestion is to change我的建议是改变

int month = c.get(Calendar.MONTH);

to

int month = c.get(Calendar.MONTH)+1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM