本文介紹了這個部門有什么問題?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
代碼如下:
class testsum
{
public static void main(String arg[])
{
double sum=0;
double fraction;
fraction=-1/9;
System.out.println("fraction: "+fraction);
fraction=-1;
fraction=fraction/9;
System.out.println("fraction: "+fraction);
}
}
輸出是 0
然后是 -0.11111111
為什么第一個輸出是 0
而不是 -0.11111111111
?
why was the first output 0
and not -0.11111111111
?
推薦答案
它在第一個示例中進行整數除法,因為這是數字文字的默認類型.嘗試將其更改為 -1.0/9
(或 1d/9d
- d 后綴表示 double
),您應該會得到相同的答案.
It's doing integer division in the first example as this is the default type for a numeric literal. Try changing it to -1.0/9
(or 1d/9d
- the d suffix indicates a double
) and you should get the same answer.
這篇關于這個部門有什么問題?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!