JavaSE_03_recursion

package homeWork;
/**
 * 实现代码:递归求N的阶乘
 * @author 天亮教育-刘世龙
 * 2022年1月1日 下午8:09:03
 */
public class work_01 {

	public static void main(String[] args) {
		int result = m1(5);
		System.out.println(result);

	}
	public static int m1(int n){
		if(n==1){
			return 1;
		}else{
			return n*m1(n-1);
		}
	}

}
package homeWork;
/**
 * 实现代码:递归求1+2+3+…+10  
 * @author 天亮教育-刘世龙
 * 2022年1月1日 下午8:16:06
 */
public class work_02 {

	public static void main(String[] args) {
		int result = m1(4);
		System.out.println(result);
	}
	public static int m1(int n){
		if (n == 1){
			return 1;
		}
		return n+ m1(n-1);
	}

}
package homeWork;

import java.util.Scanner;
/**
 * 实现代码:按顺序打印一个数字的每一位(例如1234 打印出1 2 3 4)
 * @author 天亮教育-刘世龙
 * 2022年1月2日 下午3:26:18
 */
public class work_03 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个数字,并按回车键确认:");
		m1(scanner.nextInt());
	}
	public static void m1(int number) {
		int a,x;
		a=number/10;
		x=number%10;
		if(a==0){
			System.out.print(number);	
		}else{
			m1(number/10);
			System.out.print(" "+x+" ");		
		}
 }
}

package homeWork;

import java.util.Scanner;

/**
 * 实现代码:写一个递归方法,输入一个非负整数,返回组成它的数字之和  
 * @author 天亮教育-刘世龙
 * 2022年1月2日 下午4:43:40
 */
public class work_04 {


	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个非负整数:");
		int  result = scanner.nextInt(); 
	    int sum = m1(result);
	    System.out.println(sum);
	}
	public static int m1(int number){
		if(number/10==0){
			return number;
		}else{
			return number%10 + m1(number/10);
		}			
	}

}
package homeWork;

import java.util.Scanner;

/**
 * 声明一个方法,接收一个参数
 	判断传递的参数值再斐波那契数列的第几位上
 	如果不存在 打印 -1 , 如果存在 打印对应的位数
        难点 : 如何算不存在?
            大于上一位,小于当前位  说明不存在
 * @author 天亮教育-刘世龙
 * 2022年1月2日 下午5:38:17
 */
public class work_05 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入数字:");
		m1(scanner.nextInt());
	}
	public static void m1(int n){
		int i1=1;
		int i2=1;
		int i3,count=2;
		while(true){
		i3 = i1 + i2 ;
		i1=i2;
		i2=i3;
		count++;
		if(n==i3){
			System.out.println("在斐波那契数列的第"+count+"位");
			break;
		}
		if(i3>n){
			System.out.println(-1);
			break;
		}
	}
	}

}
package homeWork;

import java.util.Scanner;

/**
 * 题目要求;
 * 比较两个数据是否相等,参数类型分别为两个byte,short,int,long类型。
 * 并在main方法中进行调试。
 * @author 天亮教育-刘世龙
 * 2022年1月2日 下午6:05:26
 */
public class work_06 {
	public static void main(String[] args) {
		
		byte b1=1;
		byte b2=1;
		
		short s1=1;
		short s2=2;
		
		int i1=2;
		int i2=2;
		
		long l1=1L;
		long l2=2L;
		
		System.out.println("b1是否与b2相等"+m1(b1,b2));
		System.out.println("s1是否与s2相等"+m2(s1,s2));
		System.out.println("i1是否与i2相等"+m3(i1,i2));
		System.out.println("l1是否与l2相等"+m4(l1,l2));
		System.out.println("b1是否与s2相等"+m2(b1,s2));
		
	}
	public static boolean m1(byte a,byte b){
		return  a ==  b;
	}
	public static boolean m2(short a,short b){
		return a == b;
	}
	public static boolean m3(int a,int b){
		return a == b;
	}
	public static boolean m4(long a,long b){
		return a == b;
	}
}

		

package homeWork;

import java.util.Scanner;

/**
 * 实现代码:按顺序打印一个数字的每一位(例如1234 打印出1 2 3 4)
 * @author 天亮教育-刘世龙
 * 2022年1月2日 下午6:34:21
 */
public class work_07 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个数字:");
		m1(scanner.nextInt());
	}
	public static void m1(int number){
		int num = number%10;
		if(number/10==0){
			System.out.print(number);
		}else{
			m1(number/10);						
		System.out.print(" "+num+" ");
		}
	}

}


 

package homeWork;

import java.util.Scanner;

/**
 * 需求 给出一个整数 判断该数是否为质数  
 * ps : 只能被1和本身整除的数 是质数
 * @author 天亮教育-刘世龙
 * 2022年1月2日 下午6:48:46
 */
public class work_08 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个数: ");
		m1(scanner.nextInt());

	}
	public static void m1(int number){
		for(int i=2;i<=number;i++){
			if(number%i==0){
				System.out.println("此数不是质数");
				break;
			}else{
				System.out.println("此数是质数");
				break;
			}
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值