day04-方法_定义注意事项

This commit is contained in:
2026-07-18 12:00:05 +08:00
parent 5d507b9376
commit 7520b05152
2 changed files with 26 additions and 0 deletions
+1
View File
@@ -20,6 +20,7 @@ public class Demo02 {
System.out.println(sum);//6
System.out.println("-------------------");
printN(10);
System.out.println("程序结束");
}
+25
View File
@@ -0,0 +1,25 @@
package com.inmind.method;
/*
学习方法的注意事项
1.方法要定义在类中方法外
2.当方法的返回值类型为void的时候,需要写return?不需要。
可以写return?可以,但是return之后不能跟任何数据,只能写return;
3.return:结束方法,跟是否返回方法的调用处无关,只要方法结束了一定返回方法调用处
4.在一个方法中可以写多个return吗?
可以,但是只能有一个return被调用
*/
public class Demo03 {
public static void main(String[] args) {
int a = 0;
if (a > 10) {
return;
} else if (a > 3 && a <= 10) {
System.out.println("a的值在3~10之间");
return;
} else {
System.out.println("a的值小于等于3");
}
System.out.println("程序结束");
}
}