Ich schreibe einen Code, wo ich bestimmen muss, um wie viel sich eine ganze Zahl in der letzten Schleife geändert hat.Vergleichen eines Int mit seinem alten Wert in Java
Mein Code:
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) { }
}
}
Zum Beispiel, ich habe ganze Zahl i. Ich möchte eine Schleife ausführen und sehen, wie viel ich geändert habe (In meinem Fall ist ich einer x-coord zugewiesen, die jede Schleife ändert). Ich setze die Ganzzahl j gleich i, sobald ich in diese Schleife gesetzt wurde. Ich muss dann ich mit j vergleichen.
Das Problem ist, dass ich j als gleich zu etwas definieren muss, um es zu initialisieren. Das heißt, wenn ich beispielsweise j initialisiere, indem ich es auf 0 setze, wird es jedes Mal, wenn mein Code eine Schleife ausführt, auf 0 zurückgesetzt, was bedeutet, dass ich i nicht mit seinem alten Wert (der j sein soll) vergleichen kann. .....
Ich versuchte, eine 'für' Anweisung zu verwenden, aber ich bekomme Fehler, weil später in meinem Code, wenn ich I und J vergleiche es sagt, dass j nicht initialisiert wurde .... Ich nehme an, weil Code in der for-Schleife wird nicht unbedingt am Anfang des Programms ausgeführt werden?
Sorry für die verwirrende Frage .... Es ist schwer für mich zu erklären!
Jede Hilfe wird sehr geschätzt !!!
ich habe versucht, das jedoch in der ‚init‘ Funktion des Codes setzen, gibt es immer noch Fehler ... –
Setze es nicht in der init-Funktion, sondern setze es nur auf den eigentlichen Klassenstart. – Ajay
'Klasse SampleListenerMain erweitert Listener { int j 0; public void onInit (Controller-Controller) {... ' – Ajay