代碼如下:import java.util.Scanner;public class Factorial { public static void main(String[] args) {
System.out.println("---開始---");
Scanner input = new Scanner(System.in); int i ; int j; int sum = 1;
System.out.print("請輸入要階乘的數字:");
i = input.nextInt();
j = i; while (i >= 1) {
sum *= i;
i--;
}
System.out.println(j+"的階乘為:"+sum);
}
}代碼測試,輸入5,6,7,8 這些都沒問題,算出來都是對的但是輸入100,99之類的 得出來結論是0是咋回事呢?如圖
1 回答

繁花不似錦
TA貢獻1851條經驗 獲得超4個贊
雖然我不學Java
,但很明顯99!===9.332622e+155
已經超出int
的取值范圍了,這就會導致結果溢出,進而產生無數個占位符零.
參考Why does Java think that the product of all numbers from 10 to 99 is 0?
添加回答
舉報
0/150
提交
取消