packagemethod;publicclassDemo01{//main 方法publicstaticvoidmain(String[] args){//实际参数:实际调用传递给他的参数int sum = add (1,2);System.out.println(sum);//test();}//加法//形式参数,用来定义作用的publicstaticintadd(int a,int b){return a+b;}}
方法定义的格式
方法定义和调用
packagemethod;publicclassDemo02{publicstaticvoidmain(String[] args){double max =max(10.0,10.0);System.out.println(max);}//比大小publicstaticdoublemax(double num1,double num2){int result =0;if(num1==num2){System.out.println("num1==num2");return0;}if(num1 > num2){
result =(int) num1;}else{
result =(int) num2;}return result;//终止方法}//比大小publicstaticintmax(int num1,int num2){int result =0;if(num1==num2){System.out.println("num1==num2");return0;}if(num1 > num2){
result = num1;}else{
result = num2;}return result;//终止方法}}