day04--方法_定义的练习

This commit is contained in:
2026-09-17 15:06:36 +08:00
parent a058d5f25c
commit c286bdbe6c
+51
View File
@@ -0,0 +1,51 @@
package com.inmind.method01;
/*
方法定义的2个明确:
1.明确返回值类型
2.明确参数列表
案例:
方法练习1_比较两个整数是否相同
方法练习2_运算1到n的累和
方法练习3_打印n遍HelloWorld
*/
public class Test02 {
public static void main(String[] args) {
//调用两个整数是否相同方法
int x = 10;
int y = 20;
boolean result = isEqual(x, y);
System.out.println(result);
//计算n的累和
int sum = getSum(100);
System.out.println(sum);
printN(5);
}
//方法练习1_比较两个整数是否相同
public static boolean isEqual(int a,int b) {
/*if (a == b) {
return true;
} else {
return false;
}*/
return a==b;
}
//方法练习2_运算1到n的累和
public static int getSum(int n) {
int sum = 0;
for (int i = 0; i <= n; i++) {
sum += i;
}
return sum;
}
//方法练习3_打印n遍HelloWorld
public static void printN(int n) {
for (int i = 0; i < n; i++) {
System.out.println("helloworld");
}
}
}