gpt4 book ai didi

java - int循环的计​​算和比较?

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:15 25 4
gpt4 key购买 nike

这是类:Main

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 。如果不同,则打印 PassIN 然后保存/保存到 LPassIN 中,如果相同,则仅打印 LPassIN 。

因此,如果有新的第三个 int 到来,它将与新的第二个 int 进行比较

是我的方法错误还是有设置规则?

*忽略 PassOUT

最佳答案

不太清楚Udp callUdp = new Udp();看起来不像java api?

关于java - int循环的计​​算和比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38365375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com