简体   繁体   English

怀疑if / else-if逻辑

[英]Doubt in if/else-if logic

I am using the following code 我正在使用以下代码

String sample = " : : ";
String[] splitTime = sample.split(":");
if ( splitTime[0].trim().equals("") ||
        splitTime[0].trim().equals(null)) {
      System.out.println("Inside Split 0");
      splitTime[0] = "0";
 } else if (splitTime[1].trim().equals("") ||
        splitTime[1].trim().equals(null)) {
      System.out.println("Inside Split 1");
      splitTime[1] = "0";
 }else if (splitTime[2].trim().equals("") ||
        splitTime[2].trim().equals(null)) {
      System.out.println("Inside Split2");
      splitTime[2] = "0";
 }

 System.out.println("Value 1 :"+splitTime[0]);
 System.out.println("Value 2 :"+splitTime[1]);
 System.out.println("Value 3 :"+splitTime[2]);

OUTPUT : 输出:
Inside Split 0 内部拆分0
Value 1 :0 值1:0
Value 2 : 值2:
Value 3 : 值3:
I don know why it is not going into second and third if-else statement. 我不知道为什么它不进入第二和第三if-else语句。 Can anyone help me? 谁能帮我?

It's because of the " else if ". 这是因为“ else if ”。

If you have code that does: 如果您有执行此操作的代码:

if (a) {
} else if (b) {
} 

Then the second branch can never be executed if the first branch was. 然后,如果第一个分支执行过,则第二个分支将永远无法执行。

Instead you need: 相反,您需要:

if (a) {
}

if (b) {
}

to make the conditions independent. 使条件独立。

BTW, String.trim() can't return null , and you're not guaranteed to get three elements in the output from String.split() . 顺便说一句, String.trim()不能返回null ,并且不能保证从String.split()的输出中获取三个元素。 Potentially better code would be: 可能更好的代码是:

String sample = " : : ";
String[] splitTime = sample.split(":");
for (int i = 0; i < splitTime.length; ++i) {
    splitTime[i] = splitTime[i].trim();
    if (splitTime[i].length() == 0) {
        splitTime[i] = "0";
    }
}

because if the first if statement is true, and thus is executed, then the rest of the else ifs are not executed. 因为如果第一个if语句为true,并因此被执行,则其余的ifs则不执行。 If you want to execute the other else-if statements, why not rewrite them as separate if statements? 如果要执行其他else-if语句,为什么不将它们重写为单独的if语句?

String sample = "::";
String[] splitTime = sample.split(":");
if ( splitTime[0].trim().equals("") ||
        splitTime[0].trim().equals(null)) {
      System.out.println("Inside Split 0");
      splitTime[0] = "0";
 }
 if (splitTime[1].trim().equals("") ||
        splitTime[1].trim().equals(null)) {
      System.out.println("Inside Split 1");
      splitTime[1] = "0";
 } 
 if (splitTime[2].trim().equals("") ||
        splitTime[2].trim().equals(null)) {
      System.out.println("Inside Split2");
      splitTime[2] = "0";
 }

 System.out.println("Value 1 :"+splitTime[0]);
 System.out.println("Value 2 :"+splitTime[1]);
 System.out.println("Value 3 :"+splitTime[2]);

This is due to the fundamental operation of an If-Else clause: 这是由于If-Else子句的基本操作所致:

if(foo) {
  // A
} else if(bar) {
  // B
}

If foo is true, A will execute, and bar will never be tested. 如果foo为true,则A将执行,并且bar将永远不会被测试。 To get the semantics you want, consider just chaining your if blocks: 要获得所需的语义,请考虑仅链接if块:

if(foo){
  // A
}

if(bar) {
  // B
}

In nested if else-if statements, if one statement is executed then others are not executed. 在嵌套的if-if语句中,如果执行一个语句,则不执行其他语句。 In your case, the first if expression is always true so it executes and thus other two never be executed. 在您的情况下,第一个if表达式始终为true,因此它会执行,因此其他两个永远不会执行。

try 尝试

String sample = " 12 : 59 : 60 ";
// strip spaces and create up to 3 parts
String[] splitTime = sample.trim().split(" *: *", 3);
for (int i = 0; i < splitTime.length; i++)
    System.out.println("Inside " + i + " '" + splitTime[i] + '\'');

Prints 版画

Inside 0 '12'
Inside 1 '59'
Inside 2 '60'

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

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