簡體   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