[英]Comparing an int to its old value in Java
我正在编写一些代码,需要确定最后一个循环中整数的变化量。
我的代码:
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时说它未初始化,所以我得到了错误。...我想是因为for循环中的代码不一定会得到在程序的开头运行?
很抱歉这个令人困惑的问题。...我很难解释!
任何帮助深表感谢!!!
没有一些示例代码就很难提供答案,但是您应该能够在循环外将先前的值初始化为初始值。
例如:
// 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
类型来表示前一个值为null的值,以表示其第一次迭代,或带有布尔标志,例如:
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;
}
如果要再次访问局部变量,则不要使用该局部变量。 如果变量不是本地变量,则无需初始化它。
例如,在循环之前,您具有int j = 0
,然后,每次循环迭代都不会重置它。
例如:
int j = 0;
for(int i=0; i<someVariable; i++){
//do code
//compare i to j
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.