2016-07-14 83 views
1

這是類:主要計算和比較int循環?

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; 
} 

} 

我想比較計算與經過的LPassIN,所以我通過將O傳遞給LPassIn來開始循環,然後LPassIN將從PassIN中保存一個新的第一個int。之後,如果新的第二個int到來,變量將比較int LPassIn和PassIN。如果不同則打印PassIN,然後保存/保存到LPassIN中,如果相同則打印LPassIN。

因此,如果新的第三INT來,將比較新的第二INT

是我的方法不對或有設置這樣的規則?

*忽略PassOUT

+0

發佈一個[SSCCE](http://sscce.org/)請求 –

回答

0

不是關於的udp callUdp =新的UDP很清楚();看起來不像java api?

+0

那是從另一個類請求數據/變量,也就是類Udp –

+0

我想執行並檢查程序,但由於Udp i不能這樣做?有沒有進口? – Fairoz

+0

ooo ok2,我編輯了所有代碼的問題..所以數據,從類中檢索:Udp是套接字編程。 主頁>轉換> Udp –