day04-方法_定义注意事项

This commit is contained in:
2026-04-25 15:45:53 +08:00
parent a795a9a9ea
commit 0c12dcedc0

View File

@@ -4,6 +4,13 @@ package com.inmind.method01;
方法练习1_比较两个整数是否相同
方法练习2_运算1到n的累和
方法练习3_打印n遍HelloWorld.
----------------------------------------------------------------
学习方法定义的注意事项:
1.方法要定义在类中方法外
2.当方法的返回值类型是void时候需要写return不需要
可以写return可以作用是提前结束方法但是不能返回任何数据
3.在一个方法中可以写多个return???
可以最终最多只有一个return会执行
*/
public class Test02 {
@@ -21,6 +28,10 @@ public class Test02 {
//方法练习3_打印n遍HelloWorld.
public static void print(int n){
if (n < 1) {
return;//提前结束方法如果n小于1则直接返回不执行下面的代码
}
for (int i = 0; i < n; i++) {
System.out.println("helloworld");
}