403- import java.util.Scanner;
- public class JPD04 {
- static Scanner keyboard = new Scanner(System.in);
- public static void main(String args[]) {
- int m,n;
- System.out.print("Input m:");
- m=keyboard.nextInt();
- while(m!=999) {
- System.out.print("Input n:");
- n=keyboard.nextInt();
- System.out.println("Ans(尾端遞迴)="+add(m,n,1));
- System.out.println("Ans(迴圈)="+round(m,n,1));
- System.out.print("Input m:");
- m=keyboard.nextInt();
- }
- }
- static int add(int m,int n,int r) {
- if(n==0)
- return r;
- else
- return add(m,n-1,r*m);
- }
-
- static int round(int m,int n,int r) {
- while(n!=0)
- {
- r*=m;
- n--;
- }
- return r;
- }
- }
複製代碼 |