2016-03-07 41 views
0

我一直负责创建一个程序,它会导致雀科机器人在一定程度上随机移动分配的时间量,同时计算物体的数量它在移动过程中检测并返回该量,然后显示。如何确保if语句执行,尽管其他正在进行的任务 - 雀科机器人

我可以让机器人随机移动,我可以让它计算它检测到的物体 - 但不是同时。

主:

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 
} 

我相信这是因为如果在芬奇机器人走动计数障碍声明,增量不能跑了。有没有办法解决这个问题?

在此先感谢。

回答

1

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