day04-方法定义的练习
This commit is contained in:
@@ -35,8 +35,9 @@ package com.inmind.method01;
|
|||||||
*/
|
*/
|
||||||
public class Demo01 {
|
public class Demo01 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int i;
|
int a = 10;
|
||||||
i = getSum(10,20);//方法调用处
|
int b = 20;
|
||||||
|
int i = getSum(a,b);//方法调用处
|
||||||
System.out.println(i);//30
|
System.out.println(i);//30
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
50
day04/src/com/inmind/method01/Demo02.java
Normal file
50
day04/src/com/inmind/method01/Demo02.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user