java基础练习

java乘法口诀实现

 1 package com.example.demo.util;
 2 
 3 /**
 4  * @Author: Lambert
 5  * @Date: 2019-01-31 22:49
 6  * @Description: 使用java打印乘法口诀
 7  * 范例:
 8  * 打印乘法口诀
 9  * 1*1=1
10  * 2*1=2  2*2=4
11  * 3*1=3  3*2=6  3*3=9
12  */
13 public class Test {
14     public static void main(String[] args) {
15         //外层作为行循环
16         for (int i = 1; i <= 3; i++) {
17             //内层作为列循环
18             for (int j = 1; j <= i; j++) {
19                 System.out.print(j + "*" + i + "=" + (j * i) + "\t");
20             }
21             System.out.println();
22         }
23     }
24 }

 

break的使用

 1 package com.example.demo.util;
 2 
 3 /**
 4  * @Author: Lambert
 5  * @Date: 2019-01-31 22:49
 6  * @Description: 需求:
 7  * 运动会跑5000米,跑到第三圈岔气了,退出比赛
 8  */
 9 public class Test {
10     public static void main(String[] args) {
11         //如果跑到第三圈岔气了
12         for (int i = 0; i < 10; i++) {
13             if (i == 3) {
14                 System.out.println("岔气了,跑不动了");
15                 break;
16             }
17             System.out.println("我跑了第" + i + "圈了");
18         }
19         System.out.println("程序结束执行");
20     }
21 }

 用where循环实现

 1 package com.example.demo.util;
 2 
 3 /**
 4  * @Author: Lambert
 5  * @Date: 2019-01-31 22:49
 6  * @Description: 需求:
 7  * 运动会跑5000米,跑到第三圈岔气了,退出比赛
 8  */
 9 public class Test {
10     public static void main(String[] args) {
11 //        如果跑到第三圈岔气了
12         int i = 1;
13         while (i <= 10) {
14             if (i == 3) {
15                 System.out.println("岔气了,跑不动了");
16                 break;
17             }
18             System.out.println("我跑了第" + i + "圈了");
19             i++;
20         }
21         System.out.println("程序结束执行");
22     }
23 }

 java实现请假之continue关键字使用

 1 package com.example.demo.util;
 2 
 3 /**
 4  * @Author: Lambert
 5  * @Date: 2019-01-31 22:49
 6  * @Description:
 7  * 需求:
 8  * 一周上五天班,周三请一天假
 9  */
10 public class Test {
11     public static void main(String[] args) {
12         for (int i = 0; i < 5; i++) {
13             if (i == 3) {
14                 System.out.println("请假了,不上班了");
15                 continue;
16             }
17             System.out.println("今天周" + i + "我上班的");
18         }
19     }
20 }

 

java 方法实现

1 修饰符 返回值类型 方法名(参数类型 参数名){
2     ...
3     方法体
4     ...
5     return 返回值;
6 }

使用方法对比两个数的大小

 1 package com.example.demo.util;
 2 
 3 /**
 4  * @Author: Lambert
 5  * @Date: 2019-01-31 22:49
 6  * @Description: 需求:
 7  * 比较两个数字的大小
 8  */
 9 public class Test {
10     public static void main(String[] args) {
11         int a = 10;
12         int b = 20;
13         //调用写好的方法
14         int result = compareNum(a, b);
15         System.out.println(result + "大");
16 
17     }
18 
19     public static int compareNum(int a, int b) {
20         //定义一个结果变量
21         int c = 0;
22         //比较a和b的大小,把大的值返回给c
23         if (a > b) {
24             c = a;
25         } else {
26             c = b;
27         }
28         //返回c
29         return c;
30     }
31 }

使用void方法直接对比不需要返回值:

 1 package com.example.demo.util;
 2 
 3 /**
 4  * @Author: Lambert
 5  * @Date: 2019-01-31 22:49
 6  * @Description: 需求:
 7  * 比较两个数字的大小
 8  */
 9 public class Test {
10     public static void main(String[] args) {
11         int a = 10;
12         int b = 20;
13         //调用写好的方法
14         compareNum(a, b);
15     }
16 
17 
18     //如果一个方法是void那么我们不需要返回值,所以不需要写return值
19     public static void compareNum(int a, int b) {
20         //比较a和b的大小,把大的值返回给c
21         if (a > b) {
22             System.out.println(a + "大");
23         } else {
24             System.out.println(b + "大");
25         }
26     }
27 }

void方法返回

 1 package com.example.demo.util;
 2 
 3 /**
 4  * @Author: Lambert
 5  * @Date: 2019-01-31 22:49
 6  * @Description: 需求:
 7  * 比较两个数字的大小
 8  */
 9 public class Test {
10     public static void main(String[] args) {
11         int a = 10;
12         int b = 20;
13         //调用写好的方法
14         compareNum(a, b);
15     }
16 
17 
18     //如果一个方法是void那么我们不需要返回值,所以不需要写return值
19     public static void compareNum(int a, int b) {
20         if (a < 0 || b < 0) {
21             System.out.println("程序被终止");
22             //程序终止
23             return;
24         }
25 
26         //比较a和b的大小,把大的值返回给c
27         if (a > b) {
28             System.out.println(a + "大");
29         } else {
30             System.out.println(b + "大");
31         }
32     }
33 }

 方法重载:

在同一个类中,函数(方法)名字一样,参数列表不一样:

1、必须是两个以上同名的方法

2、方法之间的参数组合必须不同(参数的数目不同或者参数的类型不同)

3、方法的返回值不能作为判断方法之间是否构成重载的依据

 1 package com.example.demo.util;
 2 
 3 /**
 4  * @Author: Lambert
 5  * @Date: 2019-01-31 22:49
 6  * @Description:
 7  * 方法重载实现
 8  */
 9 public class Test {
10     public static void main(String[] args) {
11         int a = 10;
12         int b = 19;
13 
14         int result = add(a, b);
15         System.out.println(result);
16     }
17 
18     public static int add(int a, int b) {
19         return a + b;
20     }
21 
22     public static double add(int a, double b) {
23         return a + b;
24     }
25 
26     public static double add(double a, double b) {
27         return a + b;
28     }
29 
30     public static double add(double a, int b) {
31         return a + b;
32     }
33     
34     public static int add(int a, int b, int c) {
35         return a + b + c;
36     }
37 
38     public static double add(double a, int b, int c) {
39         return a + b + c;
40     }
41     public static double add(double a, double b, int c) {
42         return a + b + c;
43     }
44     public static double add(double a, double b, double c) {
45         return a + b + c;
46     }
47     public static double add(double a, int b, double c) {
48         return a + b + c;
49     }
50 }

数组练习:

求极值

 1 package com.example.demo.util;
 2 
 3 /**
 4  * @Author: Lambert
 5  * @Date: 2019-01-31 22:49
 6  * @Description:
 7  * 数组:求极值
 8  */
 9 public class Test {
10     public static void main(String[] args) {
11         // 定义整数类型的数组
12         int[] arr = {12, 4, 5, 78, 911, 64, 92, 128, 996};
13         int result = max(arr);
14         System.out.println(result);
15     }
16 
17     /**
18      * 数组是引用数据类型,引用数据类型有可能发生指向问题
19      */
20     public static int max(int[] arr) {
21         int maxValue = -1;
22         //我们为了程序的严谨性,一般都判断null和0的长度的问题(下面顺序不能颠倒)
23         if (arr != null || arr.length != 0) {
24             for (int i = 0; i < arr.length; i++) {
25                 //如果遍历数组中的值比最大值大的话,需要更换
26                 if (arr[i] > maxValue) {
27                     maxValue = arr[i];
28                 }
29             }
30         }
31         return maxValue;
32     }
33 }

 

posted @ 2019-01-31 23:22  Emotiona°小吃货  阅读(144)  评论(0编辑  收藏  举报