本帖最後由 tonyh 於 2021-8-17 19:02 編輯
- import java.util.Scanner;
- public class Ch01 {
- public static void main(String[] args) {
- int n, res=1;
- Scanner s=new Scanner(System.in);
- System.out.print("輸入階層運算的值: ");
- n=s.nextInt();
- while(n>=1)
- {
- res*=n;
- n--;
- }
- System.out.println(res);
- }
- }
複製代碼- import java.util.Scanner;
- public class Ch01 {
- public static void main(String[] args) {
- int n;
- Scanner s=new Scanner(System.in);
- System.out.print("輸入階層運算的值: ");
- n=s.nextInt();
- System.out.println(f(n));
- }
-
- static int f(int n)
- {
- if(n==1)
- return 1;
- else
- return n*f(n-1);
- }
- }
複製代碼 |