diff --git a/day04/src/com/inmind/Demo01_method.java b/day04/src/com/inmind/Demo01_method.java index fe5737a..e824e91 100644 --- a/day04/src/com/inmind/Demo01_method.java +++ b/day04/src/com/inmind/Demo01_method.java @@ -32,6 +32,8 @@ package com.inmind; 案例:定义方法实现2个整数值的相加之和并返回 + + */ public class Demo01_method { public static void main(String[] args) { @@ -39,6 +41,35 @@ public class Demo01_method { int sum = addMethod(10, 20); System.out.println(sum); } + //方法练习1_比较两个整数是否相同 + public static boolean isEqual(int a,int b){ + /*if (a == b) { + return true; + }else { + return false; + }*/ +// return a==b?true:false; + return a==b; + } + //方法练习2_运算1到n的累和 + public static int getSum(int n){ + if (n < 1) { + System.out.println("您传递的参数不合法"); + return -1; + } + int sum = 0; + for (int i = 1; i <= n; i++) { + sum += i; + } + return sum; + } + //方法练习3_打印n遍HelloWorld + public static void printHW(int n){ + for (int i = 0; i < n; i++) { + System.out.println("helloworld"); + } + } + //2个整数值的相加之和并返回 diff --git a/day04/src/com/inmind/Demo02_method_notification.java b/day04/src/com/inmind/Demo02_method_notification.java new file mode 100644 index 0000000..0977172 --- /dev/null +++ b/day04/src/com/inmind/Demo02_method_notification.java @@ -0,0 +1,39 @@ +package com.inmind; +/* +学习方法的注意事项 +1.方法要定义在类中方法外 +2.当方法的返回值类型为void的时候,需要写return?不需要。 + 可以写return?可以,但是return之后不能跟任何数据,只能写return; +3.return:结束方法,跟是否返回方法的调用处无关,只要方法结束了一定返回方法调用处 +4.在一个方法中可以写多个return吗? + 可以,但是只能有一个return被调用 + */ +public class Demo02_method_notification { + public static void main(String[] args) { + if (5 > 3) { + System.out.println("1"); + return; + } + + if (2 > 3) { + System.out.println("2"); + return; + } + + if (4 > 3) { + System.out.println("3"); + return; + } + + printHW(5); + System.out.println("程序结束"); + } + + //方法练习3_打印n遍HelloWorld + public static void printHW(int n){ + for (int i = 0; i < n; i++) { + System.out.println("helloworld"); + } + } + +}