本文介紹了這個(gè)部門有什么問題?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
代碼如下:
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
為什么第一個(gè)輸出是 0
而不是 -0.11111111111
?
why was the first output 0
and not -0.11111111111
?
推薦答案
它在第一個(gè)示例中進(jìn)行整數(shù)除法,因?yàn)檫@是數(shù)字文字的默認(rèn)類型.嘗試將其更改為 -1.0/9
(或 1d/9d
- d 后綴表示 double
),您應(yīng)該會(huì)得到相同的答案.
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.
這篇關(guān)于這個(gè)部門有什么問題?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!