-2
我的因子方法工作正常,但我想從輸出數字和因子結果更改輸出。例如,如果用戶對輸出進入6說我想6 * 5 * 4 * 3 * 2 * 1 = 720,代替的6階乘是:720Java因子格式
int count, number;//declared count as loop and number as user input
int fact = 1;//declared as 1
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Please enter a number above 0:");
number = reader.nextInt(); // Scans the next token of the input as an int
System.out.println(number);//prints number the user input
if (number > 0) {
for (i = 1; i <= number; i++) {//loop
fact = fact * i;
}
System.out.println("Factorial of " + number + " is: " + fact);
}
else
{
System.out.println("Enter a number greater than 0");
}
}
使用是System.out.print ***()***不知何故在for循環 –
所以寫一個循環將數字從6降到1,用空格 - 星號分隔。你知道如何編寫一個循環。你知道如何打印。現在把你的知識放在一起。 – Andreas