290114lyp

导航

方法的定义和调动

 

修饰符 返回值类型 方法名(参数类型 参数名){

...

方法体

...

return 返回值;

}

package com.jiemo.method;
public class Shabi1 {  
   //main    
   public static void main(String[] args) {        
       //实际参数:实际调用传输给他的参数        
       int sum= add(1,2);      
       System.out.println(sum);        
       //test();   }    
       //加法    
       //形式参数,用来定义作用的    
       public static  
           int add(int a,int b){        
           return  a+b;   }    
       //一个方法只能完成一个功能  
       public static void test(){        
           for (int i=0;i<=1000;i++){            
               if (i%5==0){                
                   System.out.print(i+"\t");           }            
               if (i%(5*3)==0){                
                   System.out.println();           }       }   }}

 

package com.jiemo.method;
import javax.xml.transform.Result;
public class Shabi2 {  
   public static void main(String[] args) {        
       int max=max(10,10);        
       System.out.println(max);   }    
   //比大小    
   public static int max(int first,int second){        
       int result=0;        
       if (first==second){            
           System.out.println("相等");            
           return 0;       }        
       if(first>second){            
           result = first;        
      }else{            
           result=second;       }        
       return result;//终止方法   }}

 

posted on 2022-11-09 15:09  是芥末!日  阅读(15)  评论(0编辑  收藏  举报