仅当满足for循环中的所有条件时才中断while循环

时间:2018-10-29 17:33:47

标签: java for-loop while-loop break

仅当所有“机器人”都直立时,我才想中断while循环。 (*这些机器人是微型USB机器人)。

.upRight()在机器人站立时返回true,在机器人站立时返回false

public static boolean checkSomething() throws ... {

        while (true) {

            for (i = 0; i < bots; i++) { // bots = 2
                if (!theMainBots[i].isUpright()) {
                    ...
                    Thread.sleep(1000); 
                } 
                else {
                    return true;

                }
            }
        }

我面临的问题是,如果isUpright()方法对第一个“机器人”返回true,则所有其他机器人均未选中,并可能返回false。目的是在继续操作之前等待用户将机器人置于直立位置。

5 个答案:

答案 0 :(得分:1)

如果您要等到用户使机器人直立起来,则可以将if更改为while

while (true) {
     for (i = 0; i < bots; i++) { // bots = 2
         while(!theBots[i].isUpright()) {
             System.out.println("Please ensure I'm upright");
             Thread.sleep(500); 
         } 
     }               
     return true;     
}

这将循环遍历Array中的每个元素,并且当任何给定的bot都没有直立时,它将循环并休眠直到bot直立为止。在这种情况下,您无需进行while(true)循环:

public static boolean checkUpright() throws InterruptedException {
      for (i = 0; i < bots; i++) { // bots = 2
         while(!theBots[i].isUpright()) {
             System.out.println("Please ensure I'm upright");
             Thread.sleep(500); 
         } 
     }               
     return true;    
}

答案 1 :(得分:1)

实现此目的的一种方法是使用一个变量,该变量将确定何时退出循环。这里的问题是,您还需要将<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/Background" tools:context="activities.MainMenu"> <ListView android:id="@+id/MainListDecks" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/MainDeckNom" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginEnd="20dp" android:layout_marginBottom="22dp" android:text="Button" /> </RelativeLayout> 循环更改为for循环。这是为什么?因为您不知道刚才检查的机器人是否被移动。另外,除非您想再次检查,否则不必要进行外部循环。因此,代码将最终看起来像这样。

while

答案 2 :(得分:1)

上下文尚不完全清楚,但可能是将逻辑控制与用户交互混合在一起。也许这种方法可能行得通:

public static boolean checkUpright() throws InterruptedException {
    while (!areAllBotsUpright()) {
        System.out.println("Please ensure I'm upright");
        Thread.sleep(500);
    }
}

public static boolean areAllBotsUpright() {
   for (i = 0; i < bots; i++) {
       if (!theBots[i].isUpright()) {
           return false;     
       } 
   }

   return true;
}

答案 3 :(得分:1)

您应该首先检查所有机器人,然后对结果采取行动。不要尝试对检查循环内的结果采取行动。

此外,由于直到所有机器人都直立后代码才返回,因此该方法被错误命名并且不应返回值。

public static void waitUntilAllUpright() throws InterruptedException {
    for (;;) { // forever loop
        boolean allUpright = true;
        for (i = 0; i < bots; i++) {
            if (! theBots[i].isUpright()) {
                allUpright = false;
                break;
            } 
        }
        if (allUpright)
            return;
        System.out.println("Please ensure I'm upright");
        Thread.sleep(500); 
    } // loop back to check all bots again
}

答案 4 :(得分:0)

您可以从机器人数组创建列表,然后使用迭代器遍历该列表 如果特定的机器人是直立的,请使用iterator.remove从列表中将其删除。 外部,直到列表不为空。

Import-Csv "C:\blablabla\filename.csv" | ForEach-Object { 
    New-ADUser -Name $_.Name `
     ...
     -OtherAttributes @{ProxyAddresses= $_."ProxyAddresses"}      # Remove backtick
    New-Item -ItemType Directory -Path $_.HomeDirectory           # create home path if needed
    New-Item -ItemType Directory -Path $_.ProfilePath             # create profile path if needed
    $ACL = (Get-ACL -Path $_.HomeDirectory)                       # No need for quotes around properties if they do not contain spaces or other special characters
    $FullControlAccessRule = (New-Object System.Security.AccessControl.FileSystemAccessRule(
                                [System.Security.Principal.NTAccount]"hcc.local\$($_.samAccountName)",        # replace $UserName with correct variable
                                "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow"))
    $ACL.AddAccessRule($FullControlAccessRule)
    Set-ACL -Path $_.HomeDirectory $ACL
    # ->                                                                     # repeat for profile path if needed
}
相关问题