day04-方法_定义注意事项
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user