【问题标题】:Is it possible to insert a new condition to a while loop mid-loop in java?是否可以在java中的while循环中间循环中插入新条件?
【发布时间】:2021-07-04 15:16:03
【问题描述】:

我有一个while(notFound) 循环。此循环首先检查选项“a”中指定的内容,但是在循环中的某个点,该选项变为“b”,在这种情况下,我需要将循环更新为:

while((notFound) &&(trCollator.compare(data.getSurname("a"),current.data().getSurname("a")) == 0))

那么是否有任何语法仅在选项等于“b”时才检查第二部分?

【问题讨论】:

    标签: java loops if-statement


    【解决方案1】:

    你可以这样做:

    while((notFound)){
      if(option.equals("a") || (option.equals("b") &&(trCollator.compare(data.getSurname("a"),current.data().getSurname("a")) == 0))){
              //execute code...
        }
    }
    

    【讨论】:

    • 您还应该在 else 块中添加 break 语句以使其更合适,这样当两个条件(whileif)都为假时,循环应该终止.
    【解决方案2】:

    您可以使用布尔暗示来调整附加条件:
    »*If ... then ...*« 与!... || ... 相同。

    String option = "a";
    while (notFound && (!"b".equals(option) ||
           trCollator.compare(data.getSurname("a"), current.data().getSurname("a")) == 0))
    {
      // do stuff
    }
    

    【讨论】:

      【解决方案3】:

      只需使用一个标志来说明您是否需要检查它。

      boolean flag = false;
      while (notFound &&
             (!flag || trCollator.compare(data.getSurname("a"),current.data().getSurname("a") == 0)) {
      
           ...
           if (somethingOrOther)
               flag = true;
           ...
      
      }
      

      最初,flag 为假,所以!flag 为真,因此逻辑或运算符的第二个子句被跳过。后来!flag为假,所以第二个子句被求值。

      【讨论】:

        【解决方案4】:
        // create an interface
        public interface myInterface {
            public boolean compareTo();
        }
        
        // create two classes 
        public class aa implements myInterface {
        
                @Override
                public boolean compareTo() {
                    // TODO Auto-generated method stub
                    return true;
                }
        }
        
        public class bb implements myInterface {
            String surname;
            String currentSurname;
            
            bb(String surname, String currSurname) {
                this.surname = surname;
                this.currentSurname = currSurname;
            }
            
            @Override
            public boolean compareTo() {
                // TODO Auto-generated method stub
                return this.surname.equals(this.currentSurname);
            }
        }
        
        // in while loop when you hit some condition change the value of mObj like below
        
                Scanner n = new Scanner(System.in);
                int tmp =n.nextInt();
                
                myInterface mObj = new aa();
                
                while(tmp > 0 && mObj.compareTo()) {
                    // some condition
                    if(tmp == 5) {
                        mObj = new bb("kumar", "sharma");
                    }
                    tmp--;
                    
                    System.out.println(tmp);
                }
                
                System.out.println("Done");
        

        【讨论】:

        • 事实上你可以在 bb 类的 compareTo 函数中添加任何类型的比较。或者对于每种类型的比较可以有任何相似的类。
        猜你喜欢
        • 2015-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        • 2021-11-28
        • 2017-02-25
        • 2016-03-30
        相关资源
        最近更新 更多