-
何谓方法
-
定义、调用
-
方法重载
-
命令行传参
-
可变参数
-
递归
MethodDemo01
public class Demo01 { //main方法 public static void main(String[] args) { //实际参数:实际调用传递给他的参数 double calculation = sum(1,2,4); System.out.println(calculation); //test(); } //加法 //形式参数,用来定义作用的 public static int add(int a,int b){ return a+b; } public static double sum(double a,double b,double c){ return a*b*c; } public static void test(){ for (int i = 0; i <= 1000; i++) { if(i%5==0){ System.out.print(i+"\t"); if(i%3==0){ System.out.print("\n"); } } } } }
Demo02
public class Demo02 { public static void main(String[] args) { double max = max(10, 20); System.out.println(max); } //比大小 public static double max(double num1, double num2) { double result = 0; if (num1 == num2) { System.out.println("num1==num2"); return 0;//终止方法 } if (num1 > num2) { result = num1; } else { result = num2; } return result; } //比大小 public static int max(int num1,int num2){ int result = 0; if(num1==num2){ System.out.println("num1==num2"); return 0;//终止方法 } if (num1 > num2){ result = num1; }else{ result = num2; } return result; } }
Demo03
public class Demo03 { public static void main(String[] args) { //args.lenth 数组长度 for (int i = 0; i < args.length; i++) { System.out.println("args["+ i + "]:" +args[i]); } } //点击Terminal javac com.kuangstudy.method.Demo03 /*Demo03右键 open in Explorer cmd空格 回车 Javac Demo03.java cd../ java com.kuangstudy.method.Demo03 this is kuang */ }
Demo04
public class Demo04 { public static void main(String[] args) { Demo04 demo04 = new Demo04(); demo04.test(1,2,3,4,45,6); } public void test(int x,int... i){ System.out.println(i[0]); System.out.println(i[1]); System.out.println(i[2]); System.out.println(i[3]); System.out.println(i[4]); System.out.println(i[5]); } }
Demo05
public class Demo05 { public static void main(String[] args) { //调用可变参数的方法 printmax(3,45,6,78); printmax(new double[]{1,2,3,4}); } public static void printmax(double... numbers){ if(numbers.length == 0){ System.out.println("No argument passed"); return; } double result = numbers[0]; for (int i = 1; i < numbers.length ; i++) { if(numbers[i] > result){ result = numbers[i]; } } System.out.println("The max value is"+result); } }
Demo06
public class Demo06 { public static void main(String[] args) { System.out.println(f(5)); } public static int f(int n){ if(n==1){ return 1; }else{ return n*f(n-1); } } }
Demo07
import java.util.Scanner; import static java.lang.System.in; public class Demo07 { public static void main(String[] args) { while(true){ Scanner sc = new Scanner(System.in); System.out.println("请输入要操作的选项:1 加法 2 减法 3乘法 4除法"); String str = sc.next(); char select = str.charAt(0); switch (select){ case'1': System.out.println("请输入第一个数:"); double a=sc.nextDouble(); System.out.println("请输入第二个数:"); double b=sc.nextDouble(); double sum = add(a,b); System.out.println("两个数的和为:" + sum); break; case'2': System.out.println("请输入被减数:"); double c=sc.nextDouble(); System.out.println("请输入减数:"); double d=sc.nextDouble(); double difference = subtract(c,d); System.out.println("两个数的和为:" + difference ); break; case'3': System.out.println("请输入第一个数:"); double e=sc.nextDouble(); System.out.println("请输入第二个数:"); double f=sc.nextDouble(); double ride = multiply(e,f); System.out.println("两个数的和为:" + ride); break; case'4': System.out.println("请输入被除数:"); double i=sc.nextDouble(); System.out.println("请输入除数:"); double j=sc.nextDouble(); double except = divide(i,j); System.out.println("两个数的和为:" + except); break; default: System.out.println("你的输入有误,请重新输入:"); continue;//碰到continue,回到循环开始 } System.out.println("是否继续?是请输入Y,输入其他退出"); String input = sc.next(); char go = input.charAt(0); if(go != 'Y'){ break; } } } public static double add(double a,double b){ return a+b; } public static double subtract(double a,double b){ return a-b; } public static double multiply(double a,double b){ return a*b; } public static double divide(double a,double b){ return a/b; } }