方法的過載 可變引數 遞迴(重要)
阿新 • • 發佈:2020-12-17
package methods; public class Demo01 { //main方法 public static void main(String[] args) { double max=max(20.0,10.0); System.out.println(max); } //比大小 public static int max(int num1,int num2){ int result; if(num1>num2){ result=num1; }else{ result=num2; } return result; } public static double max(double num1,double num2){ double result; if(num1>num2){ result=num1; } else{ result=num2; } return result; } }
packagemethods; public class Demo03 { public static void main(String[] args) { Demo03 demo03=new Demo03(); demo03.test(1); } public static void test(int...i){ System.out.println(i[0]); } }
遞迴!!!
package methods; public class Demo04 { public static void main(String[] args) { System.out.println(f(3)); } public static int f(int n){ if(n==1){ return 1; } else{ return n*f(n-1); } } }