1 package day05; 2 3 public class MethodDemo01 { 4 /*方法定义的格式: 5 * public static void 方法名(){ 6 * 方法体 7 * } 8 * 方法的调用格式: 9 * 方法名(); 10 * 注意:方法与方法之间是平级关系,不能嵌套定义*/ 11 public static void main(String[] args) { 12 eat(); 13 } 14 15 16 public static void eat() { 17 study(); 18 System.out.println("eating"); 19 } 20 21 public static void study() { 22 System.out.println("learning"); 23 } 24 }
执行结果:
eg:
1 package day05; 2 3 public class MethodDemo02 { 4 public static void main(String[] args) { 5 method(); 6 } 7 8 public static void method() { 9 int num = 16; 10 if (num % 2 == 0) { 11 System.out.println("偶数"); 12 } else { 13 System.out.println("奇数"); 14 } 15 } 16 }
执行结果: