day04-方法定义的练习

This commit is contained in:
2026-01-16 15:08:48 +08:00
parent c98641a98f
commit 26e2479db0
2 changed files with 53 additions and 2 deletions

View File

@@ -35,8 +35,9 @@ package com.inmind.method01;
*/
public class Demo01 {
public static void main(String[] args) {
int i;
i = getSum(10,20);//方法调用处
int a = 10;
int b = 20;
int i = getSum(a,b);//方法调用处
System.out.println(i);//30
}

View File

@@ -0,0 +1,50 @@
package com.inmind.method01;
public class Demo02 {
public static void main(String[] args) {
boolean result = isEqual(4, 4);
System.out.println(result);
int lh = getLH(10);
System.out.println(lh);
printN(5);
}
//案例1比较两个整数是否相同
public static boolean isEqual(int a,int b){
//实现一
/*if (a == b) {
return true;
} else {
return false;
}*/
//实现二
/*boolean result = a==b? true:false;
return result;*/
// return a==b? true:false;
//实现三
return a==b;
}
//案例2运算1到n的累和
public static int getLH(int n) {
int result = 0;//累和
for (int i = 0; i <= n; i++) {
result += i;
}
return result;
}
//案例3打印n遍HelloWorld.
public static void printN(int n){
for (int i = 0; i < n; i++) {
System.out.println("helloWorld");
}
}
}