class functiondemo{
public static void main(String[] args){
//调用函数
int red = add(1,2);
System.out.println(red);
printemtytriangle(5);
printemtytriangle(7);
int mx2 = getmax(1,2,3);
System.out.println(mx2);
__________________________________
//函数套用
int max3 =getmax1(4,5);
max3 =getmax1(max3,8);// getmax1(getmax1(4,5),5); 函数调用函数
System.out.println(max3); //System.out.println(getmax1(getmax1(4,5),5));
System.out.println(fabric(5));
hello();
}
_____________________________________
//定义函数add,两个数相加
public static int add( int a ,int b){
int c =a+b;
return c;
}
____________________________________________________
//定义了printemtytriangle打印空心三角形的函数
public static void printemtytriangle (int line){
for (int i =0 ; i<line;i++){
for(int j =0; j<line-1-i;j++){
System.out.print(" ");
}
System.out.print("*");
if (i ==(line -1)){
for(int j = 0; j<2*i;j++){
System.out.print("*");
}
}
if ( i !=0 && i!=(line-1)) {
for(int j =0; j<2*i -1;j++){
System.out.print(" ");
}
System.out.print("*");
}
System.out.print("
");
}
}
_____________________________________________________
public static int getmax(int a1,int b1,int c1){
return (a1 > b1 ? (a1>c1 ? a1:c1) : (b1>c1 ? b1:c1));
}
____________________________________________________________
//四个数比较,函数套用
public static int getmax1(int a2,int b2){
return (a2 > b2? a2:b2);
}
______________________________________________
//阶乘
public static int fabric(int n){
int sum = 1;
for (int i = 1 ; i<=n ; i++){
sum =sum * i;
}
return sum;
}
__________________________________________________
//递归需要终止条件
public static void hello(){
System.out.print("hello how are you");
//hello();//自己调用自己跟死循环的区别在于会出现异常
}
_________________________________________________
//阶乘的第二种方法,但是需要终止条件
//n*(n-1)*(n-1-1)....
public static int fabric2(int n){
if (n ==1){
return1;
}
return n*fabric(n-1);
}
}