java - 尽管有其他正在进行的任务,如何确保 if 语句的执行 - finch 机器人

标签 java eclipse

我的任务是创建一个程序,该程序将引导芬奇机器人在指定的时间内随机移动,同时计算它在移动过程中检测到的物体数量并返回该数量以进行显示。

我可以让机器人随机移动,并且可以让它对检测到的物体进行计数 - 但不能同时进行。

主要:

    int millsec = 5000;
    int obstacleOccur = finchRandom(millsec);
    System.out.println(obstacleOccur);

方法:

static public int finchRandom(int x)
{
    Finch myf = new Finch();
    Random rand = new Random();
    int obs = 0;
    long time = System.currentTimeMillis()+x;

    while(time - System.currentTimeMillis() > 0)
    {
        if (myf.isObstacle())
        {
            obs++; //this counts the obstacles
            System.out.println("Obstacle");
        }   //below activates the wheels to move randomly,
            //the function is setWheelVelocities(leftWheel,rightWheel,duration)
            myf.setWheelVelocities(rand.nextInt(150)-75,rand.nextInt(150)-75,rand.nextInt(x/2));        
    }
    return obs; //returns the count of obstacles
}

我相信这是因为当芬奇机器人四处移动时,无法运行用于计数障碍物的 if 语句和增量。有什么办法可以解决这个问题吗?

提前致谢。

最佳答案

答案是多线程编程,您的工作是弄清楚如何使用 THread 或 Runnable 或 lambda 表达式来执行此操作。因为任何给定的线程一次只能做一件事,而您需要一次至少做两件事。

关于java - 尽管有其他正在进行的任务,如何确保 if 语句的执行 - finch 机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35856311/

相关文章:

java - Swing 应用程序窗口在 Mac 上没有响应

java - 如何编辑Java平台包(内置API)源代码?

java - 更改字体大小时遇到​​问题

java - 为一个模型创建多个 DTO 是一个好习惯吗?

java - 如何解决 pom.xml 中的错误 - android eclipse 应用程序?

Eclipse - 大纲 View - 关闭过滤

Java:如何多次阅读 Reader?

php - 如何在 php 中将 Android 签名数据分成单独的字符串

eclipse - org.eclipse.swt.SWTError : Item not added

java - 多个递归方法调用的各种堆栈大小