有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java WHILE循环计数

在java中,只要输入设备返回较低的值,如何让while循环运行

double leftDist = distanceSensorLeft.getDistance(DistanceUnit.MM);
double rightDist = distanceSensorRight.getDistance(DistanceUnit.MM);
leftDrive.setTargetPosition(leftDrive.getCurrentPosition() - 1500);
rightDrive.setTargetPosition(rightDrive.getCurrentPosition() + 1500);
leftDrive.setPower(0.2);
rightDrive.setPower(-0.2);
while (leftDrive.isBusy() && rightDrive.isBusy() && opModeIsActive() && rightDist/* value is smaller than previous value*/  && leftDist// value is smaller than previous value  ) {

}

共 (1) 个答案

  1. # 1 楼答案

    您可以将最后一个值保存在变量中,然后将其与实际值进行比较

     boolean continueBool= true;
     double lastValue=0;
     double leftDist = distanceSensorLeft.getDistance(DistanceUnit.MM);
     double rightDist = distanceSensorRight.getDistance(DistanceUnit.MM);
     leftDrive.setTargetPosition(leftDrive.getCurrentPosition() - 1500);
     rightDrive.setTargetPosition(rightDrive.getCurrentPosition() + 1500);
     leftDrive.setPower(0.2);
     rightDrive.setPower(-0.2);
    
     while (continueBool) {
            if(lastValue<=actualValue){
            continueBool=false;
        }
    
            lastValue=actualValue;
    
    }