day04-方法的定义练习&方法注意事项

This commit is contained in:
2025-12-25 12:00:14 +08:00
parent 6cd0a4a2bc
commit 79f1a412f7
2 changed files with 70 additions and 0 deletions

View File

@@ -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");
}
}
}