有 Java 编程相关的问题?

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

java LeJOS NXT编程

我是一个编程的初学者,正在尝试制作一个清洁机器人NXT i连接(超声波传感器)和(声音传感器) 机器人的工作是,当我拍手时,它必须开始向前移动,当超声波传感器看到路上有东西时,它必须转身继续前进。 问题是,当它转动时,它不会一直向前移动,直到我再次鼓掌

这是我写的代码:

public static void main(String[] args) {
  // TODO Auto-generated method stub

  TouchSensor touch = new TouchSensor(SensorPort.S2);

  SoundSensor sound = new SoundSensor( SensorPort.S4 );

  UltrasonicSensor sonic = new UltrasonicSensor( SensorPort.S3);

  Motor.A.setSpeed( 400 );
  Motor.C.setSpeed( 400 );
  Button.waitForAnyPress();

  int SoundValue;
  SoundValue = sound.readValue();
  System.out.print(SoundValue);

  do {
    if ( sound.readValue() > 50 ) {
      // PROBLEM:       
      while ( sonic.getDistance() > 30 ){
        Motor.B.backward();
        Motor.A.backward();
        Motor.C.backward();
      }
      {
        Motor.A.rotate( -185, true );
        Motor.C.rotate( 185, true );    
      }  
    };
  }

  while( Button.readButtons() != Button.ID_ESCAPE );
}

谁能帮我解决这个问题

无论如何


共 (1) 个答案

  1. # 1 楼答案

    他们认为这个循环有点错误

    基本上,我认为你需要一个标志来指示机器人应该移动,这样当你鼓掌时,它就会翻转标志

    boolean move = false;
    do {
        if ( sound.readValue() > 50 ) {
            move = !move;
        }
    
        while ( sonic.getDistance() > 30 ){
            Motor.B.backward();
            Motor.A.backward();
            Motor.C.backward();
        }
        if (move) {
            Motor.A.rotate( -185, true );
            Motor.C.rotate( 185, true );   
        }
    } while( Button.readButtons() != Button.ID_ESCAPE );
    

    或者类似的东西。否则,它只有在有另一个声音时才会移动

    我还想说,我很嫉妒;)