day04-方法的定义练习&方法注意事项
This commit is contained in:
@@ -32,6 +32,8 @@ package com.inmind;
|
||||
|
||||
案例:定义方法实现2个整数值的相加之和并返回
|
||||
|
||||
|
||||
|
||||
*/
|
||||
public class Demo01_method {
|
||||
public static void main(String[] args) {
|
||||
@@ -39,6 +41,35 @@ public class Demo01_method {
|
||||
int sum = addMethod(10, 20);
|
||||
System.out.println(sum);
|
||||
}
|
||||
//方法练习1_比较两个整数是否相同
|
||||
public static boolean isEqual(int a,int b){
|
||||
/*if (a == b) {
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}*/
|
||||
// return a==b?true:false;
|
||||
return a==b;
|
||||
}
|
||||
//方法练习2_运算1到n的累和
|
||||
public static int getSum(int n){
|
||||
if (n < 1) {
|
||||
System.out.println("您传递的参数不合法");
|
||||
return -1;
|
||||
}
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
//方法练习3_打印n遍HelloWorld
|
||||
public static void printHW(int n){
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println("helloworld");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//2个整数值的相加之和并返回
|
||||
|
||||
Reference in New Issue
Block a user