diff --git a/day02/src/com/inmind/method03/Demo14.java b/day02/src/com/inmind/method03/Demo14.java new file mode 100644 index 0000000..7659ce0 --- /dev/null +++ b/day02/src/com/inmind/method03/Demo14.java @@ -0,0 +1,49 @@ +package com.inmind.method03; +/* +方法定义格式(照抄主方法即可) + +格式: +方法修饰符 返回值类型 方法名() +{ + +} + +1.方法修饰符:public static (固定,照抄) +2.返回值类型:方法的返回值的数据类型,void表示没有返回值类型(固定,照抄void) +3.方法名:标识符中的一种,符合软性规范:符合小驼峰命名方式 methodHelloWord +4.():方法参数,参数可以写0或多个(固定,什么都不写:没有参数) +5.{java代码}:方法体,就是java语句的集合,也就是方法被调用后,要执行的java代码!!! + +调用格式:方法名(方法参数); +*/ +public class Demo14 { + + + public static void main(String[] args) { + //调用获取最大值的方法 + getMax(); + } + /* + 比较3个值的最大值(方法) + 方法的位置:类中,方法外 + */ + public static void getMax(){ + //想法,使用三元运算符,判断2次,得出最大值 + int a = 10; + int b = 40; + int c = 20; + + //1.定义一个变量,保存最大值 + int max; + + //2.两两比较,得出a,b,比较大的值,保存到最大值中 + max = a>b?a:b; + + //3.拿当前最大值与c比较,比较大的值,继续保存到最大值中 + max = max > c? max:c; + + System.out.println("最大值max:"+max); + + } + +}