代码之家  ›  专栏  ›  技术社区  ›  Gabriel Ruoff

在Java中将int与其旧值进行比较

  •  0
  • Gabriel Ruoff  · 技术社区  · 10 年前

    我正在编写一些代码,我需要通过一个整数在最后一个循环中发生了多少变化来确定。

    我的代码:

    import gnu.io.CommPort;
    import gnu.io.CommPortIdentifier;
    import gnu.io.SerialPort;
    import com.leapmotion.leap.*;
    
    import java.io.IOException;
    
    import com.leapmotion.leap.Gesture.State;
    import java.io.OutputStream;
    
    import com.leapmotion.leap.Controller;
    import com.leapmotion.leap.Frame;
    import com.leapmotion.leap.Listener;
    import java.lang.Math;
    
    class SampleListenerMain extends Listener {
    
    public void onInit(Controller controller) {
    System.out.println("Initialized");
    }
    
    public void onConnect(Controller controller) {
    System.out.println("Connected");
    controller.enableGesture(Gesture.Type.TYPE_SWIPE);
    controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
    controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
    controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
    
    
    }
    
    public void onDisconnect(Controller controller) {
    System.out.println("Disconnected");
    }
    
    public void onExit(Controller controller) {
    System.out.println("Exited");
    }
    public void onFrame(Controller controller) {
    
    
    Frame frame = controller.frame();
    
    Hand hand = frame.hands().rightmost();
    float positionx = hand.palmPosition().getX();
    System.out.println("X: " + positionx);
    
    float positiony = hand.palmPosition().getY();
    System.out.println("Y:" + positiony);
    
    float positionz = hand.palmPosition().getZ();
    System.out.println("Z: " + positionz);
    
    System.out.println("Hands: " + frame.hands().count());
    
    int in_min = -117;
    int in_max = 117;
    int out_min = 900;
    int out_max = 1900;
    int in_min2 = -73;
    int in_max2 = 73;
    
    int positionmapx = (int) ((positionx - in_min) * (out_max - out_min) /     (in_max - in_min)) + out_min;
    int positionmapy = (int) ((positiony - in_min) * (out_max - out_min) / (in_max - in_min)) + out_min;
    int positionmapz = (int) ((positionz - in_min) * (out_max - out_min) / (in_max2 - in_min)) + out_min;
    
    
    if (frame.hands().count() == 0) {
        LeapDrone.writeToArduino("0");
    }
    
    
    int posmapxold = 0;
    int posmapyold = 0;
    int posmapzold = 0;
    
    
    String data = Integer.toString(positionmapy)+":"+     Integer.toString(positionmapx)+":"+Integer.toString(positionmapz);
    
    System.out.println("data: " + data);
    
    
    System.out.println("Old X: " + posmapxold);
    
    System.out.println("Difference X: " + Math.abs(positionmapy - posmapyold));
    
    if (Math.abs(positionmapx - posmapxold) >= 25) {
    
    
        LeapDrone.writeToArduino(data);
    
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
    
            e.printStackTrace();
        }
    
    }
    
    if (Math.abs(positionmapy - posmapyold) >= 25) {
    
    
        LeapDrone.writeToArduino(data);
    
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    if (Math.abs(positionmapz - posmapzold) >= 25) {
    
    
        LeapDrone.writeToArduino(data);
    
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    posmapxold = positionmapx;
    posmapyold = positionmapy;
    posmapzold = positionmapz;
    
    System.out.println("New-Old X: " + posmapxold);
    
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    GestureList gestures = frame.gestures();
    for (int i = 0; i < gestures.count(); i++) {
        Gesture gesture = gestures.get(i);
    
    
    
    }
    }
    }
    
       class LeapDrone {
    static OutputStream out = null;
    
    public static void main(String[] args) {
    //Connect to Arduino BT
    
    try
    {
    (new LeapDrone()).connect("/dev/cu.usbmodem1421");
    }
       catch ( Exception e )
    {
        e.printStackTrace();
    System.exit(0);
    }
    
    // Create a sample listener and controller
    SampleListenerMain listener = new SampleListenerMain();
    Controller controller = new Controller();
    
    // Have the sample listener receive events from the controller
    controller.addListener(listener);
    // Keep this process running until Enter is pressed
    System.out.println("Press Enter to quit...");
    try {
    System.in.read();
    } catch (IOException e) {
    e.printStackTrace();
    }
    
    // Remove the sample listener when done
    controller.removeListener(listener);
    }
    
    void connect ( String portName ) throws Exception {
    
    CommPortIdentifier portIdentifier =     CommPortIdentifier.getPortIdentifier(portName);
    if ( portIdentifier.isCurrentlyOwned() )
    {
    System.out.println("Error: Port is currently in use");
    }
    else
    {
    CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
    
    if ( commPort instanceof SerialPort )
    {
    SerialPort serialPort = (SerialPort) commPort;
    serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE);
    out = serialPort.getOutputStream();
    }
    else
    {
    System.out.println("Error: Only serial ports are handled by this example.");
    }
    }
    }
    
    public static void writeToArduino(String data)
    {
    String tmpStr = data;
    byte bytes[] = tmpStr.getBytes();
    try {
    out.write(bytes);
    } catch (IOException e) { }
    }
    }
    

    例如,我有一个整数I。我想运行一个循环,看看我改变了多少(在我的例子中,我被分配给一个x坐标,它会改变每个循环)。一旦在循环中设置了I,我就将整数j设置为I。然后我需要将I与j进行比较。

    问题是我必须将j定义为等于某个值来初始化它。这意味着,例如,如果我通过将j设置为0来初始化j,那么每当我的代码循环时,它都会重置为0,这意味着我无法将I与它的旧值(应该是j)进行比较。。。。。。

    我尝试使用“for”语句,但我得到了错误,因为稍后在我的代码中,当我比较I和j时,它表示j尚未初始化……我想是因为for循环中的代码不一定在程序开始时运行?

    对不起,这个令人困惑的问题……我很难解释!

    非常感谢任何帮助!!!

    2 回复  |  直到 10 年前
        1
  •  1
  •   Mark    10 年前

    如果没有一些示例代码,很难提供答案,但您应该能够将之前的值初始化为循环外的初始值。

    例如:

    // Initialize our previous value to be the current value right off the bat
    previousValue = currentValue;
    
    for (int index = 0; index < whatever; index++) {
    
       // Do something that changes the value
       currentValue = someNewValue();
    
       // Calculate our delta
       delta = currentValue - previousValue;
    
       // Set our previous value to the new current value
       previousValue = currentValue;
    
    }
    

    如果由于某种原因,currentValue只能在循环中计算,那么您可以通过使用Object类型为第一次迭代添加一个特例 Integer 而不是原语 int 为前一个值键入一个空值,以表示其第一次迭代,或键入一个布尔标志,如下所示:

    boolean firstIteration = true;
    
    for (int index = 0; index < whatever; index++) {
    
       currentValue = someNewValue();
    
       if (firstIteration) {
    
          // Our first iteration has a zero delta
          delta = 0;
          firstIteration = false;
    
       } else {
    
          // Subsequent iterations compare the previous value
          delta = currentValue - previousValue;
    
       }
    
       previousValue = currentValue;
    
    }
    
        2
  •  0
  •   Ajay Jack Shen    10 年前

    如果希望在局部变量再次循环时访问它,则不应使用它。如果变量不是局部变量,则无需对其进行初始化。

    例如,在循环之前 int j = 0 ,则每次循环迭代都不会重置它。

    前任:

    int j = 0;
    for(int i=0; i<someVariable; i++){
        //do code
    
        //compare i to j
    }