day04--方法_定义注意事项

This commit is contained in:
2026-09-17 15:13:54 +08:00
parent c286bdbe6c
commit 213f041cd3
+28
View File
@@ -0,0 +1,28 @@
package com.inmind.method01;
/*
学习方法定义的注意事项:
1.方法要定义在类中方法外
2.当方法的返回值类型是void时候,需要写return?不需要
可以写return?可以,作用是提前结束方法,但是不能返回任何数据
3.在一个方法中可以写多个return???
可以,最终最多只有一个return会执行
*/
public class Demo03 {
public static void printN(int n) {
if (n == 0) {
System.out.println("你在耍我?0次还要打印吗?");
return;
}
if (n > 1000) {
System.out.println("你想累死我,打印1000遍,CPU不累吗??");
return;
}
for (int i = 0; i < n; i++) {
System.out.println("helloworld");
}
}
}