繁体   English   中英

多个if语句和一个循环

[英]multiple if statements and a loop

我有一个逻辑问题。 我将提供伪代码,因为真正的代码不那么可读。 我只想输入其中一条指令,如果没有一条指令可访问,我希望函数从循环中退出。

我对编程非常陌生,因此不胜感激。

编辑:在每次迭代中,我只想达到条件之一。 由于循环会持续较长的时间,因此我希望每次迭代都能达到相同/其他指令

while(true){
     if(condition){ // Instruction 1
        do stuff
    }else{
        do stuff
    }
    if(condition){ // Instruction 2
        do stuff
    }else{
        do stuff
    }
    if(condition){ // Instruction 3
        do stuff
    }else{
        do stuff
    }

    if(condition){ // Instruction 4
        do stuff
    }else{
        do stuff
    }

    if(none condition){
        break;
    }
}

我认为到目前为止还没有人知道,所以我会加深对您所要询问的理解。

while(true) // keep looping
{
    boolean instructionExecuted = false; // we haven't done an instruction this iteration

    // Instruction 1
    if(condition) {                         
        instructionExecuted = true;
        //do stuff
    } else {
        //do stuff
    }

    // Instruction 2
    if(condition && !instructionExecuted) {  // If we've not done an instruction,
                                             // and we can do instruction #2
        instructionExecuted = true;
        //do stuff
    } else if (!instructionExecuted) {       // only do this if we haven't already
                                             // done an instruction
        //do stuff
    }

    // Instruction 3
    if(condition && !instructionExecuted) { // If we've not done an instruction,
        instructionExecuted = true;         // and we can do instruction #3
        //do stuff
    } else if (!instructionExecuted) {      // only do this if we haven't already
                                            // done an instruction
        //do stuff
    }

    //etc.

    if(none condition)
    {
        break;
    }
}

您可能正在寻找添加布尔触发器。 如果输入任何if语句,则将触发器设为true。

boolean reachable = false;
while(true){
    if(condition){ // Instruction 1
        do stuff
        reachable = true
    }else{
        do stuff
    }
    if(condition){ // Instruction 2
        do stuff
        reachable = true
    }else{
        do stuff
    }
    if(condition){ // Instruction 3
        do stuff
        reachable = true
    }else{
        do stuff
    }

    if(condition){ // Instruction 4
        do stuff
        reachable = true
    }else{
        do stuff
    }

    if(!reachable){
        break;
    }
}

您正在使它变得比必须的更加困难。 您需要做的就是在其后加上一个if语句和多个else if语句。

例:

if (condition) {
    doStuff();
} else if (condition) {
    doStuff();
} else if (condition) {
    ...
} else {
    break;
}

好吧,有多种方法可以实现您想要的:

  • 引入一个顶级布尔标志doNotBreak并在if / else块的执行中设置为true,这意味着循环不应中断。
  • 通过在循环中使用合并条件来检查是否中断: if(condition1 && !condition2 && ..) break;
  • 代替最后一个条件,使用条件中断,并在不想中断的if / else块内,使用continue

另外,您可以检查是否可以使用switch语句代替if-else。

        public static void main(String argv[]) {
          String [] conditions = {"condition_1","condition_2","condition_3","condition_4","xyz"};
          printConditionType(conditions);
        } 
        public static void printConditionType(String [] conditions){
          int i = 0;
          while (i<conditions.length){
             switch (conditions[i]) {
                 case "condition_1":
                    System.out.println("#1");
                    break;
                 case "condition_2":
                    System.out.println("#2");                    
                    break;
                 case "condition_3":
                    System.out.println("#3");                    
                    break;
                 case "condition_4":
                    System.out.println("#4");
                    break;
                 default:
                    System.out.println("Invalid condition:" + conditions[i]);
            }
            i++;
        }
    }

暂无
暂无

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

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