关于java基础_方法的简单习题

 1 package day05;
 2 
 3 import java.util.Arrays;
 4 
 5 /**
 6  * 方法作业
 7  * @author ASUS
 8  *
 9  */
10 public class Demo6 {
11 /*
12  * 1、定义一个方法,求圆的面积
13       方法参数:半径 (radius)
14       圆周率:Math.PI
15    2.定义一个方法,求数组的最大值
16 
17 
18  */
19     public static void main(String[] args) {
20        //1.
21         int r;
22         circle(r=3);
23        
24        //2.
25        int arr1[] = {12,34,34,54};
26        
27        max1(arr1);
28        //3.
29        int arr2[] ={21,34,45,23,44,12,56};
30        order(arr2);
31        //4.
32        int arr3[] ={21,34,45,23,44,12,56};
33        onOrder(arr3);
34     }
35     
36     
37     //圆的面积
38     public static void circle(int r) {
39         double s=0;
40         s=(r*r*Math.PI);
41         System.out.println(s);
42         return;
43     }
44     //数组最大值
45     public static void max1(int arr1[]){
46         int m=arr1[0];
47         for(int i=0;i<arr1.length;i++){
48             
49             if(m<arr1[i]){
50                 m=arr1[i];
51                 
52             }
53             
54         }
55         
56         System.out.println("最大值为"+m);
57     }
58     
59     /*
60      * 3、定义一个方法,对数组进行排序(冒泡排序)
61      */
62     public static void order(int arr2[]){
63         for(int i=0;i<arr2.length;i++){
64             for(int j=0;j<arr2.length-i-1;j++){
65                 if(arr2[j]>arr2[j+1]){
66                     int temp=arr2[j];
67                     arr2[j]=arr2[j+1];
68                     arr2[j+1]=temp;
69                 }
70             }
71                 }
72         for(int str:arr2){
73             System.out.println(str);
74         }
75     }
76     
77     /*
78      * 4、定义一个方法,完成数组的逆序
79      */
80     public static void onOrder(int arr3[]){
81         
82         for(int i=0;i<arr3.length/2;i++){
83             int temp=arr3[i];
84             arr3[i]=arr3[arr3.length-1-i];
85             arr3[arr3.length-1-i]=temp;
86             
87         }
88         System.out.println("该数组逆序为"+Arrays.toString(arr3));
89     }
90     
91     
92 }

/*
* 5、编写程序,定义三个重载方法并调用。方法名为calculate。
a)三个方法分别接收一个int参数、两个int参数、一个字符串参数。分别执行平方运算并输出结果,相乘并输出结果,输出字符串信息。
b)在main方法中分别调用三个方法。
c)
2.定义三个重载方法max,第一个方法求两个int值中的最大值,第二个方法求两个double值中的最大值,
第三个方法求三个double值中的最大值,并分别调用三个方法。
*/

 1 package day05;
 2 
 3 public class Demo7 {
 4 
 5     public static void main(String[] args) {
 6         
 7         calculate(3);
 8         calculate(4,5);
 9         calculate("少年强则国强!");
10         
11         max(221,221);
12         max(22.1,11.22);
13         max(12.1,32.1,22.2);
14     }
15     /*
16      * 5、编写程序,定义三个重载方法并调用。方法名为calculate。
17         a)三个方法分别接收一个int参数、两个int参数、一个字符串参数。分别执行平方运算并输出结果,相乘并输出结果,输出字符串信息。
18         b)在main方法中分别调用三个方法。
19         c)
20         2.定义三个重载方法max,第一个方法求两个int值中的最大值,第二个方法求两个double值中的最大值,
21                                   第三个方法求三个double值中的最大值,并分别调用三个方法。
22      */
23     
24     //平方
25     public static void calculate(int a){
26         int squale;
27         squale=a*a;
28         System.out.println("平方后为"+squale);
29     
30     }
31     //相乘
32     public static void calculate(int a,int b){
33         int multiplied;
34         multiplied=a*b;
35         System.out.println("相乘后"+multiplied);
36     
37     }
38     //字符串
39         public static void calculate(String m){
40             
41             System.out.println(m);
42         
43         }
44         
45         public static void max(int a,int b){
46             if(a>b){
47                 System.out.println("最大值为"+a);
48             }else{
49                 System.out.println("最大值为"+b);
50             }
51         }
52        public static void max(double a,double b){
53            if(a>b){
54                System.out.println("最大值为"+a);
55            }else{
56                System.out.println("最大值为"+b);
57            }
58        }
59        public static void max(double a,double b,double c){
60            if(a<b){
61                if(b<c){
62                    System.out.println("最大值为"+c);
63                }else{
64                    System.out.println("最大值为"+b);
65                }
66                
67            }else{
68                if(b<c){
69                    System.out.println("最大值为"+a);
70                }
71            }
72        }
73 }
原文地址:https://www.cnblogs.com/yumu77/p/13687820.html