如何为所有这些语句使用循环 [英] How to I use a loop for all these statements

查看:68
本文介绍了如何为所有这些语句使用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一种让机器人以特定形状骑行的方法。在骑行期间,他必须检测到碰撞并在他发现障碍物时停止。

当我为此使用循环时,它只会在方法开始时检测到碰撞,而不会在其余命令中检测到。如何在不为每一个动作制作循环的情况下将此循环应用于每个动作?



我尝试过的方法:



5代表5厘米检测长度



if(!Detect.obstacle(5))

{

forwardLong();

Drive.toTheRight();

forwardShort();

Drive.toTheLeft();

forwardLong();

Drive.toTheLeft();

forwardShort();

Drive.toTheLeft();

forwardLong();

Drive.toTheLeft();

forwardShort();

Drive.toTheRight();

forwardLong();

Drive.toTheRight();

forwardShort(); < br $>
}

其他

{

BoeBot.wait(5);

州= 1;

}

I am writing a method to make a robot ride in a specific shape. During the riding he must detect collision and stop if he has detected an obstruction.
When I would use a loop for this, it would only detect collision at the start of the method, and not during the rest of the commands. How can I make this loop apply to every movement without making a loop for every single movement?

What I have tried:

5 stands for 5 centimers detection length

if(!Detect.obstacle(5))
{
forwardLong();
Drive.toTheRight();
forwardShort();
Drive.toTheLeft();
forwardLong();
Drive.toTheLeft();
forwardShort();
Drive.toTheLeft();
forwardLong();
Drive.toTheLeft();
forwardShort();
Drive.toTheRight();
forwardLong();
Drive.toTheRight();
forwardShort();
}
else
{
BoeBot.wait(5);
state = 1;
}

推荐答案

我这样做的方法是使用一组命令。

这可能就像一串字符串一样简单字符对:一个用于操作,一个用于持续时间。

也许是这样的:

The way I'd do it is to use a set of commands.
This could be as simple as a string of character pairs: one for operation, one for duration.
Perhaps like this:
FLDRFSDLFL...

F表示向前,L表示长; D代表方向,R代表右; F表示前进,S表示短; D表示方向,L表示左; ...等等。

然后让你的循环处理一对字符:如果它得到FL它会调用 forwardLong ;如果它获得DL,则调用 Drive.toTheLeft 在每个命令之后,它会检查障碍物,然后再转到下一个命令。



这也意味着您不必为每个形状重写代码 - 只需更改传递它的字符串。

Where "F" is for "forward", "L" is for "Long"; "D" is for "Direction", "R" is for "Right"; "F" is for "forward", "S" is for "Short"; "D" is for "Direction", "L" is for "Left"; ... and so on.
Then have your loop process a pair of characters: if it gets "FL" it calls forwardLong; if it gets "DL" it calls Drive.toTheLeft After each command, it checks for an obstruction before moving on to the next command.

This also means that you don't have to rewrite your code for each shape - just change the string you pass it.


您的问题不是很清楚。但我建议为一次移动传递距离和方向作为参数并返回碰撞状态的方法:

Your question is not very clear. But I suggest to write a method for a single move passing distance and direction as parameters and returning the collision state:
boolean MoveOne(boolean longMove, boolean leftDirection)
{
    // Optionally wait and check again here upon collision
    if (IsCollision())
        return false;
    if (longMove)
        forwardLong();
    else
        forwardShort();
    if (leftDirection)
        Drive.toTheLeft();
    else
        Drive.toTheRight();
    return true;
}

然后,您可以定义一个移动数组并使用循环为每个项目对调用该函数:

You can then define an array of movements and use a loop to call that function for each item pair:

// Tip: Define constants for the corresponding moves to increase readability
boolean moveShape[][] = { 
    { true, false}, { false, true} // , ...
};
for (int i = 0; i < numberOfMoves; ++)
{
    if (!MoveOne(moveShape[i][0], moveShape[i][1]))
    {
        break;
    }
}



我建议看看bot移动为对象(我现在没有java环境,所以修复语法):



您应该定义类如



I suggest to look at bot moves as objects (I have no java environment now, so fix the syntax) :

You should define the class like

class SingleMove
{
    public SingleMove(boolean short, boolean left)
    {
    Short = short;
    Left = left;
    }
        public boolean Short;
        public boolean Left;
    }

public void Move()
{
       if (Short)
    {
       forwardShort();
    }
    else
    {
      forwardLong();
    }

if (Left)
{
  Drive.toTheLeft();
}
else
{
  Drive.toTheRight();
}


}


}



比你可以创造具有首选运动算法的数组:



ArrayList [] Allmoves = new Arralylist;



Allmoves.Add (new SingleMove(True,False));



...



Allmoves.Add(new SingleMove(True,False));

Allmoves.Add(new SingleMove(False,False));



然后你的行动代码将如下:




Than you can create array with preferred algorithm of movement:

ArrayList [] Allmoves = new Arralylist;

Allmoves.Add(new SingleMove(True, False));

...

Allmoves.Add(new SingleMove(True, False));
Allmoves.Add(new SingleMove(False, False));

Then your action code will be like :

for(int ii = 0 ; ii <  Allmoves.GetLenght(); ii++)
{
   if(!Detect.obstacle(5))
    {
        Allmoves[ii].Move();
    }
}


这篇关于如何为所有这些语句使用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆