一.
题目要求:
定义一个方法,用来判断两个数字是否相同。
public class Method01Same {
public static void main(String[] args) {
System.out.println(isSame(2,2));
System.out.println("---------------");
System.out.println(isSame(1,2));
}
/*
三要素:
返回值类型:boolean
方法名称:isSame
参数列表:int a, int b
*/
public static boolean isSame(int a , int b){
/*
boolean same; ---第一种写法
if(a == b){
same = true;
}else {
same = false;
}
return same;
*/
/*
boolean same = a == b? true : false; ---第二种写法
return same;
*/
/*
boolean same = a == b; ----第三种写法
return same;
*/
return a == b; //-------第四种写法
}
}
二.
题目要求:
定义一个方法,用来求出1-100之间所有数字的和值。
public class Method02Sum {
public static void main(String[] args) {
System.out.println("结果是" + getSum());
}
/*
返回值:有返回值,计算结果是一个int
方法名称:getSum
参数列表:数据范围已经确定,是固定的,所以不需要告诉我任何条件,不需要参数
*/
public static int getSum(){
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
return sum;
}
}
三.
题目要求:
定义一个方法,用来打印指定次数的HelloWorld.
public class Method03Print {
public static void main(String[] args) {
printCount(100);
}
/*
三要素
返回值类型:只是进行打印操作,没有进行计算,也没有返回值
方法名称:printCount
参数列表:到底打印多少次?必须告诉我,否则我不知道打印多少次。次数,int num;
*/
public static void printCount(int num){
for (int i = 0; i <= num; i++) {
System.out.println("Hello,World!!!" + i );
}
}
}
Java方法实现案例

被折叠的 条评论
为什么被折叠?



