我已經被要求創建一個程序,它將接收一個數字並且每個循環取1,直到它達到0,然後輸出每個數字和它們乘以找到fractorial並顯示它的工作,例如5 * 4 * 3 * 2 * 1 = 120
例如,如果用戶進入5的輸出應是
5 * 4 * 3 * 2 * 1 = 120
我嘗試沒有運氣
int factor = sc.nextInt();
int count = 0;
int total = factor;
StringBuilder answer = new StringBuilder();
answer.append(factor);
while(factor>0)
{
for(int i = factor; i >= 0; i--)
{
count++;
total = total*count;
total = total -1;
if(i==factor)
{
answer.append(" = ").append(total);
}
else
answer.append(" * ").append(String.valueOf(i));
}
}
int sanswer=Integer.parseInt(answer.toString());
return sanswer;
}
以下
我想你說的是階乘,你可以看到這個:http://stackoverflow.com/questions/891031/is-there-a-method-that-calculates-a-factorial-in-java –
是的謝謝我試圖找到一個問題,但沒有找到一個解決方案生病看看 – studentprogrammer