Java方法

 

 1 package method;
 2 
 3 public class Demo1 {
 4     //main方法
 5     public static void main(String[] args) {
 6        int sum=  add(1,2 );
 7         System.out.println(sum);
 8 
 9     }
10 
11     //加法
12     public  static int add(int a,int b){
13         return a+b;
14     }
15 }
 1 package method;
 2 
 3 public class Demo2 {
 4     public static void main(String[] args) {
 5         int  max=max(10,20);
 6         System.out.println(max);
 7 
 8     }
 9 
10     //比大小
11     public static int max(int num1,int num2){
12 
13         int result=0;
14 
15         if (num1==num2){
16             System.out.println("num1==mun2");
17             return 0;//终止方法
18         }
19 
20         if(num1>num2){
21             result = num1;
22         }else {
23             result = num2;
24         }
25         return  result;
26     }
27 }

 

posted on 2021-05-06 20:33  帆吖  阅读(42)  评论(0)    收藏  举报