java 阶乘递归_java--带参方法 递归阶乘

本文详细探讨了Java中的递归和参数传递。通过示例代码解释了基本类型的值传递以及引用类型的地址传递,展示了如何使用引用参数计算数组的和。此外,还介绍了可变参数的使用和递归的概念,包括递归实现阶乘的方法。
摘要由CSDN通过智能技术生成

packagecom.test.day01;//public classTestParam {public void f1(intn){

n=0;

}public static voidmain(String[] args) {

TestParam testParam= newTestParam();int n = 9;

testParam.f1(n);

System.out.println(n);

}

}

思考:上面的代码运行结果输出的是0还是9  答案是9    因为调用一个方法就会在栈里面开辟空间,这时候0在一个空间 9在一个空间。那么结果不会随着方法的调用而改变

那么什么时候会改变呢?

packagecom.test.day01;//classParam{intvalue;

}public classTestParam {

//讲Param类作为类型public voidf2(Param p){

p.value=99;

}public static voidmain(String[] args) {

TestParam testParam= newTestParam();

Param pc= newParam();

pc.value=22;

testParam.f2(pc);

System.out.println(pc.value);

}

}

思路:引用传递是地址的传递,这里面使用了同一个地址,也就是都指向99 所以结果是99  如果在 f2里面再次创建一个对象的话,就会在堆里面创建对象,就会有新的地址。结果就会是22

使用引用参数求5个数的和:

packagecom.test.day01;//

public classTestParam {//创建一个求和的方法

public int num(int n1,int n2,int n3,int n4,intn5){return n1+n2+n3+n4+n5;

}public static voidmain(String[] args) {//求5个数的和

TestParam tp = newTestParam();int sum = tp.num(88, 99, 54, 78, 90);

System.out.println(sum);

}

}

上述的代码虽然完成5个数的求和,但是如果遇到很多个数求和那么显然这样的方式是不可取的。那么怎么解决呢?传参的时候使用引用数据类型数组然后for循环的方式;

packagecom.test.day01;//

public classTestParam {//创建一个求和的方法//定义一个求和的方法

public int sum(int[] arr){int sum =0;for (int i = 0; i < arr.length; i++) {

sum+=arr[i];

}returnsum;

}public static voidmain(String[] args) {

TestParam testParam= newTestParam();int [] a={6542,3,5,7,4,2,4,67,8,3,56,8,534,3,2,6,6,8,9,4,2};int num =testParam.sum(a);

System.out.println(num);

}

}

定义一个数组,存储3名学员的基本信息,编写方法计算学员的总成绩。

packagecom.test.day01;classStudent{//姓名 性别 年龄 分数

doublescoer;charsex;

String name;intage;

}//管理类

classManager{public doublesum(Student [] stus){int sum =0;for (int i = 0; i < stus.length; i++) {

sum+=stus[i].scoer;

}returnsum;

}

}public classTestStudent {public static voidmain(String[] args) {//TODO Auto-generated method stub

Student xiaolongnv = newStudent();

xiaolongnv.scoer=100;

Student mucheng= newStudent();

mucheng.scoer=84.5;

Student zzhangruocheng= newStudent();

zzhangruocheng.scoer=69.3;

Student [] stus={xiaolongnv,mucheng,zzhangruocheng};double num = newManager().sum(stus);

System.out.println(num);

}

}

可变参数

可变参数的底层是数组  他的优点是传参灵活,注意多个参数的时候可变参数只能放在最后。

public classTestVarParam {public void f(String s ,int [] arr,int[] arr1) {

System.out.println(Arrays.toString(arr));

}public void ff(int ...arr ) {//int []arr

System.out.println(Arrays.toString(arr));

System.out.println(arr.length);

}public static voidmain(String[] args) {

TestVarParam test= newTestVarParam();//1//int [] arr = {11,22,33};//int [] arr = new int[]{11,22,33};//test.f(arr);//2//test.f(new int [] {11,22,33});//3.

test.ff();//new int []{}; 长度为0 的数组

test.ff(111);//new int[]{111}

test.ff(11,22,33,44);//new int[]{11,22,33,44};

test.ff(new int[] {11,22});

}

}

递归

递归就是方法自身调用自身,但是一定要有出口

packagecom.test.day01;public classRecursion {int count=0;public voidf(){

count++;if(count==10){return;

}

System.out.println("慢慢学,这要学的快");

f();

}public static voidmain(String[] args) {//使用递归的方法输出10个“慢慢学,这要学的快”

Recursion re = newRecursion();

re.f();

}

}

使用递归实现阶乘

packagecom.test.day01;public classFactorial {public int fa(intn){if(n==1){return 1;

}else{return n*fa(n-1);

}

}public static voidmain(String[] args) {//使用递归实现阶乘 阶乘形式:1*2*3*4*5*6*7

Factorial factorial = newFactorial();

System.out.println(factorial.fa(6));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值