这是课程:主要
public class Main {
int secondPassed = 0;
Timer mytime = new Timer();
TimerTask mytask = new TimerTask()
{
public void run() {
secondPassed++;
if (secondPassed == 3){
secondPassed = 0;
Convertion callConvertion = new Convertion();
try {
callConvertion.cnvrt();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int donecnvrtin = callConvertion.donecnvrtin();
int donecnvrtout = callConvertion.donecnvrtout();;
System.out.println("IN : " + donecnvrtin);
System.out.println("OUT : " + donecnvrtout);
}
else{
System.out.println("Second : " + secondPassed);
}
}
};
public void start(){
mytime.scheduleAtFixedRate(mytask, 1000,1000);
}
public static void main(String[] args) throws Exception {
Time starter = new Time();
starter.start();
}
}
这是类:Udp
public class Udp {
public String result;
public void socketp() throws Exception {
DatagramSocket clientSocket = new DatagramSocket(10076);
InetAddress IPAddress = InetAddress.getByName("192.168.100.10");
byte[] sendData = new byte[9];
byte[] receiveData = new byte[9];
String sentence = "VDV2bE1";
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 10076);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
clientSocket.close();
this.result = new String(receivePacket.getData());
return;
}
public String trnsfrRPckt(){
return this.result;
}
}
这是课程:转换
public class Convertion {
public int PassIN;
public int PassOUT;
public String convertStringToHex(String str){
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hex.append(Integer.toHexString((int)chars[i]));
}
return hex.toString();
}
public void cnvrt(){
Udp callUdp = new Udp();
try {
callUdp.socketp();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String trnsferRP = callUdp.trnsfrRPckt();
Convertion strToHex = new Convertion ();
String hex = strToHex.convertStringToHex(trnsferRP);
String cut = hex.substring(10);
String A = cut.substring(0, 2);
String B = cut.substring(2, 4);
String C = cut.substring(4, 6);
String D = cut.substring(6, 8);
int cvA = Integer.parseInt(A);
int cvB = Integer.parseInt(B);
int cvC = Integer.parseInt(C);
int cvD = Integer.parseInt(D);
int calcA = (cvA - 30) * 16;
int calcB = (cvB - 30);
int calcC = (cvC - 30) * 16;
int calcD = (cvD - 30);
this.PassIN = calcA + calcB;
this.PassOUT = calcC + calcD;
int tempIN = 0;
int tempOUT = 0;
int LPassIN;
int LPassOUT;
if (tempIN == 0){
LPassIN = PassIN + tempIN;
System.out.println(LPassIN);
}else if (LPassIN != PassIN){
LPassIN = PassIN;
System.out.println(PassIN);
}else{
System.out.println(LPassIN);
}
if (tempOUT == 0){
LPassOUT = PassOUT + tempOUT;
System.out.println(LPassOUT);
}else if (LPassOUT != PassOUT){
System.out.println(PassOUT);
}else{
System.out.println(LPassOUT);
}
}
public int donecnvrtin(){
return this.PassIN;
}
public int donecnvrtout(){
return this.PassOUT;
}
}
我正在尝试将计算的PassIN与LPassIN进行比较,因此我首先将O传递给LPassIn进行循环,然后LPassIN将保存PassIN中的新第一个int。此后,如果有新的第二个int,则变量将比较两个Int LPassIn如果不同,则打印PassIN,然后将其保存/保存到LPassIN中;如果相同,则仅打印LPassIN。
因此,如果有新的第三个整数,它将与新的第二个整数进行比较
我的方法有误还是有规则设置?
*忽略PassOUT
最佳答案
对Udp不太清楚callUdp = new Udp();看起来不像Java API吗?
关于java - 计算并比较int循环?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38365375/