0

結果を表示しようとしていますが、答えを計算してもゼロではなく、ゼロの後に小数点以下が必要ですが、0を返すだけです

コード :

                long transferRate = ((len*2) / durationseconds ) ; //amount of data in bytes transferred in 1 second. Currently returning 0 for every result
                System.out.println("transferRate: " + transferRate + " bytes per second");

小数点以下の桁数までの回答を返す必要がある部分です。これを指定するにはどうすればよいですか?

現在、結果が表示されているため:

Time for the ping to be sent and recived of 2 bytes is 365636931 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 4 bytes is 43375591 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 8 bytes is 51079641 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 16 bytes is 54751211 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 32 bytes is 57195731 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 64 bytes is 48524461 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 128 bytes is 51243251 seconds
transferRate: 0 bytes per second

ありがとう

4

1 に答える 1

3
long transferRate = ((len*2) / durationseconds ) ;

に小数を格納することはできませんlong。試す:

double transferRate = ((len*2) / (double) durationseconds ) ;

オペランドの 1 つを a にキャストする必要がありますdouble。そうしないと、とにかく整数除算を行うことになります。

たとえば、1 / 2は と評価され0ます。しかし、1 / 2.0でしょう0.5

于 2013-04-13T22:29:14.117 に答える