- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一些代码,我需要确定最后一个循环中整数发生了多少变化。
我的代码:
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 来初始化它,那么每次我的代码循环时它都会重置为 0,这意味着我无法将 i 与其旧值(应该是 j)进行比较。 ......
我尝试使用“for”语句,但出现错误,因为稍后在我的代码中,当我比较 i 和 j 时,它说 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 只能在循环中计算,那么您可以使用对象类型 Integer
而不是原始 int< 为第一次迭代添加特殊情况
为前一个值键入一个 null 值以表示其第一次迭代,或者使用一个 boolean 标志,如下所示:
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;
}
关于java - Java 中 int 与其旧值的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36490140/
几周前,我安装了一个新的 ssl 证书来替换一个过期的证书。 .crt 和 .key 的文件名保持不变。只是内容变了。 现在,当我访问我的网站时,它说证书已过期,我看到它正在使用旧的证书链。我可以确认
这个问题在这里已经有了答案: Make namespaces backwards compatible in PHP (4 个回答) 8 年前关闭。 我真的很想在我的工作流程中采用命名空间。据我了解,
我从 CN1 开始,然后以 Todo App 为例。当我在 Netbeans 中运行 de app 时,只出现一个空白表单,我更改了主题,添加了一个 jpg 图像并使用旧的 GUI Builder 在
我想知道是否有一些 Laravel 人员可以帮忙。 我有一个表单,其中有 2 个单选按钮,当表单提交时,它会通过验证器,如果验证器失败,它会返回表单,使用输入填充字段并显示错误消息。 我似乎无法对单选
我正在使用 Java 中没有泛型的旧代码。 hashMap 是在该代码中定义的,没有泛型,例如: Map A = new HashMap(); 在这段代码中,我想如何确定要在键和值中放入什么内容,以及
我想问一个关于项目兼容性的问题。我的论文项目是在Windows Vista和XP上在JRE 1.6和JDK 1.6下开发的。该项目使用Java新套接字。今天我尝试在 Windows 8(64 位)机器
有谁知道一个脚本可以将旧的 Products 命名空间样式 Plone 附加组件包装到 Egg 中吗? 生成setup.py 创建目录结构 还需要采取其他措施吗? 鸡蛋化过程中有什么陷阱吗? 最佳答案
我已经运行 MySQL 5.6.12 一段时间了。我决定将我的 WAMP 服务器更新到最新的 PHP 版本。 在此过程中,它还将 MySQL 更新到 5.6.17,保留 .12 数据,但无法访问。 如
最近,我一直在尝试根据互联网上的各种旧教程编写论坛代码,但是我最近遇到了一个问题 - 尽管我完全按照教程所述进行操作,但我收到了空格错误。我认为这可能是因为某些 MySQL 命令可能已更改。如果有人可
我正在创建对 Count 表的查询。 $Month = $_POST['Month']; $query = "SELECT ANY_VALUE(AD) AS ad, COU
如果我使用 mysqldump 工具备份旧版本的 MySql 数据库,是否存在任何已知风险?例如,如果我在生产机器上使用 mysqldump 5.6 来备份 MySql 5.X 数据库。 最佳答案 有
当将 columnsData 值分配给 columns 时,我有两个 JSON 对象 columnsData 和 columns,这两个值都会更改。 var columnsData = [
我有一个需要在 gcc 4.4 上编译的多线程应用程序,我不允许使用 c++0x 标志。 我希望一个变量以原子方式运行,但不幸的是没有 C++0x 标志我无法使用 atomic在 C++ 中。 我试过
我可以借助广播事件(ACTION_TIME_CHANGED 和 ACTION_DATE_CHANGED)获取时间更改事件。 我需要在时间更改后获取之前的时间。例如,当前时间是 10:00。我要把时间改
我正在尝试在我的 Android 应用程序中创建一个 DatePickerDialog,但是当我创建一个 DatePickerDialog 时,我收到以下消息:Call requires API le
{!! Form::open(array('route' => 'posts.store', 'data-parsley-validate' => '')) !!} {{ Form::labe
我的问题与 iOS 周围的蓝牙技术有关。我看过关于蓝牙低功耗 101、新功能、基础知识等的 WWDC,以及关于使用 iOS 5 及更高版本中可用的 CoreBluetooth 框架的内容。我浏览了不同
我有一个有五个屏幕的应用。 在每个屏幕上,我在 viewDidLoad 中从服务器访问数据。 在每个屏幕上我都有下一个按钮。 当我从屏幕一转到屏幕五(通过单击下一步 4 次)时,在 NSLog 中,我
我最近在一家网络报纸找到了一份工作。在网站上,我们有一个非常古老且重要的 Symfony 应用程序,它是为一位年长的开发人员编写的,已经消失很久了。该应用程序是神圣的:是报纸收入的血液。问题是我们没有
我相信我已经找到了一种方法来实现类似可移植 C89 中众所周知的“struct hack”的方法。我很好奇这是否真的严格符合 C89。 主要思想是:我分配足够大的内存来容纳初始结构和数组元素。确切的大
我是一名优秀的程序员,十分优秀!