检查 IF 条件,然后将其传递到 ELSE。

huangapple go评论65阅读模式
英文:

Checking IF condition then bypass it to ELSE

问题

我有一段IF ELSE条件语句的代码。假设这是我的代码。

如何在不更改A或B的情况下进入isTrue的IF ELSE代码段?
我尝试过return true,但由于它在方法中,所以不会返回。有没有任何关键字可以这样做?

public void test() {
 if (text1.equals("") {
   //输出错误消息
 } else if (text2.equals("") { 
   //输出错误消息
 .
 .
 .
 } else if (A!= null && (A > B))  {
   //在不更改A或B的情况下进入ELSE条件
 } else { if (isTrue){} }
}
英文:

I have a block of IF ELSE condition statement. Let's say that this is my code

How do I get to the IF ELSE of isTrue of code without changing the A or B?
I tried return true but It wouldn't return because it's in a method. Is there any keyword to do so?

public void test() {
 if (text1.equals("") {
   //Output error message
 } else if (text2.equals("") { 
   //Ouput error message
 .
 .
 .
 } else if (A!= null && (A > B))  {
   //GO TO ELSE condition without changing the A or B
 } else { if (isTrue){} }
}

答案1

得分: 1

将最后一个条件从其他条件中分离出来:

public void test() {
   if (text1.equals("")) {
      //输出错误消息
   } else if (text2.equals("")) { 
      //输出错误消息
   } else if (A != null && (A > B))  {
      //转到ELSE条件,不改变A或B
   }
   
   if (isTrue) { }
}
英文:

Take the last conditional out of the other conditionals:

public void test() {
   if (text1.equals("") {
      //Output error message
   } else if (text2.equals("") { 
      //Ouput error message
   } else if (A!= null && (A > B))  {
      //GO TO ELSE condition without changing the A or B
   }

   if (isTrue){ }
}

答案2

得分: 0

看起来你想要使用这个方法返回一个布尔值来检查任何参数...
如果是这样的话,可以这样做:

public boolean testParams() {
    // 检查文本1
    if (text1.equals("this shouldnt be inside")) {
        System.out.println("This is an error text 1 is not what expected");
        return false;
    }

    // 检查文本2
    if (text2.equals("this shouldnt be inside")) {
        // 做一些操作
        .
        .
        .
        System.out.println("This is an error text 2 is not what expected");
        return false;
    }
        
    if (A < B && isTrue)
        return true;

    // 在其他情况下返回false
    return false;
}

如果它应该是一个没有返回值的方法(即void方法),可以这样做:

public void testParams() {
    // 检查文本1
    if (text1.equals("this shouldnt be inside")) {
        System.out.println("This is an error text 1 is not what expected");
        return;
    }

    // 检查文本2
    if (text2.equals("")) {
        // 做一些操作,然后在最后返回一个错误
        .
        .
        System.out.println("This is an error text 2 is not what expected");
        return;
    }
        
    if (A < B) {
        // 当A小于B时做一些操作
        if (isTrue) {
            // 当参数isTrue为true时做一些操作...
        } else {
            // 当参数isTrue为false时做另一些操作...
        }
    } else {
        // 当A大于B时做一些操作
    }
}
英文:

Looks for me like you try to return a boolean with that method to check any parameter...
If thats true use:

public boolean testParams() {
// check text 1
if (text1.equals(&quot;this shouldnt be inside&quot;) {
    System.out.println(&quot;This is an error text 1 is not what expected&quot;);
    return false;
}

// check test2
if (text2.equals(&quot;this shouldnt be inside&quot;) {
    // do sth
    .
    .
    .
    System.out.println(&quot;This is an error text 2 is not what expected&quot;);
    return false;
}
    
if(A &lt; B &amp;&amp; isTrue)
    return true;

// in any other case return false
return false;

}

If it should be a void ( so a method without any returning value) do this:

public void testParams() {

// check text 1
if (text1.equals(&quot;this shouldnt be inside&quot;) {
    System.out.println(&quot;This is an error text 1 is not what expected&quot;);
    return;
}

// check test2
if (text2.equals(&quot;&quot;) {
    // do sth and then at the end return with an error
    .
    .
    System.out.println(&quot;This is an error text 2 is not what expected&quot;);
    return;
}
    
if(A &lt; B) {
    // do something when A is smaller then B
    if(isTrue) {
        // do sth when parameter isTrue is true...
    } else {
        // do another action when parameter isTrue = false...
    }
} else {
    // do something when A is greater then B
}    

}

huangapple
  • 本文由 发表于 2020年10月21日 16:40:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64459883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定