本文介绍了java中的错误划分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在划分两个int值,我希望得到一个双倍的值。但它的工作原理很奇怪,它在分割之前有正确的值,但它没有给出正确的答案。
I am dividing two int values, and im expecting to get a double one. but it works very strange, it has the correct values just before division but it doesnt give the right answer.
public void Analyse() {
for (FlowPacket fp : this.flow.GetAll()) {
if (fp.direction==1){
this.sentPackets++;
this.sentLength = this.sentLength + fp.packetLength;
}
else{
this.receivedPackets++;
this.receivedLength = this.receivedLength + fp.packetLength;
}
}
if(this.receivedPackets==0)
this.receivedPackets = 1;
}
public double CalcRatio() {
return (this.sentPackets/this.receivedPackets);
}
---------------- ------------ main --------------------------------
----------------------------main--------------------------------
System.out.print("Sent packets: " + analyser.getTotalSentPackets() + " , ");
System.out.print("Received packets: " + analyser.getTotalReceivedPackets() + " , ");
System.out.print("ratio: " + analyser.CalcRatio() + " , ");
--------------------- ------- outout ------------------------------
----------------------------outout------------------------------
Sent packets: 2694 , Received packets: 5753 , ratio: 0
推荐答案
(double)this.sentPackets/this.receivedPackets
...应该修复它。
这篇关于java中的错误划分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!