计算并比较int循环?

时间:2016-07-14 04:15:15

标签: java sockets if-statement

这是课程:主要

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.If不同然后打印PassIN然后保存/保持到LPassIN,如果相同然后只打印LPassIN。

因此,如果有新的第三个int,它将与新的第二个int

进行比较

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

*忽略PassOUT

1 个答案:

答案 0 :(得分:0)

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