首页 技术 正文
技术 2022年11月6日
0 收藏 815 点赞 807 浏览 7207 个字

1、递归求5的阶乘

  1. package com.heima.chario;
  2. public class Demo8_Digui {
  3. /**
  4. * @param args
  5. * 递归:方法自己调用自己
  6. * 5!
  7. * 5 * 4 * 3 * 2 * 1
  8. *
  9. * 5 * fun(4)(代表4!)
  10. * 4 * fun(3)(代表3!)
  11. * 3 * fun(2)(代表2!)
  12. * 2 * fun(1)(代表1!)
  13. * 递归的弊端:不能调用次数过多,容易导致栈内存溢出
  14. * 递归的好处:不用知道循环次数
  15. *
  16. * 构造方法是否可以递归调用?
  17. * 构造方法不能使用递归调用
  18. *
  19. * 递归调用是否必须有返回值?
  20. * 不一定(可以有,也可以没有)
  21. */
  22. public static void main(String[] args) {
  23. /*int result = 1;
  24. for(int i = 1; i <= 5; i++) {
  25. result = result * i;
  26. }
  27. System.out.println(result);*/
  28. System.out.println(fun(6000));
  29. }
  30. public static int fun(int num) {
  31. if(num == 1) {
  32. return 1;
  33. }else {
  34. return num * fun(num - 1);
  35. }
  36. }
  37. }

2、从键盘接收一个文件夹路径,统计该文件夹大小

  1. package com.heima.test;
  2. import java.io.File;
  3. import java.util.Scanner;
  4. public class Test1 {
  5. /**
  6. * @param args
  7. * 需求:1,从键盘接收一个文件夹路径,统计该文件夹大小
  8. *
  9. * 从键盘接收一个文件夹路径
  10. * 1,创建键盘录入对象
  11. * 2,定义一个无限循环
  12. * 3,将键盘录入的结果存储并封装成File对象
  13. * 4,对File对象判断
  14. * 5,将文件夹路径对象返回
  15. *
  16. * 统计该文件夹大小
  17. * 1,定义一个求和变量
  18. * 2,获取该文件夹下所有的文件和文件夹listFiles();
  19. * 3,遍历数组
  20. * 4,判断是文件就计算大小并累加
  21. * 5,判断是文件夹,递归调用
  22. */
  23. public static void main(String[] args) {
  24. //File dir = new File("F:\\day06");
  25. //System.out.println(dir.length());//直接获取文件夹的结果是0
  26. File dir = getDir();
  27. System.out.println(getFileLength(dir));
  28. }
  29. /*
  30. * 从键盘接收一个文件夹路径
  31. * 1,返回值类型File
  32. * 2,参数列表无
  33. */
  34. public static File getDir() {
  35. //1,创建键盘录入对象
  36. Scanner sc = new Scanner(System.in);
  37. System.out.println("请输入一个文件夹路径:");
  38. //2,定义一个无限循环
  39. while(true) {
  40. //3,将键盘录入的结果存储并封装成File对象
  41. String line = sc.nextLine();
  42. File dir = new File(line);
  43. //4,对File对象判断
  44. if(!dir.exists()) {
  45. System.out.println("您录入的文件夹路径不存在,请输入一个文件夹路径:");
  46. }else if(dir.isFile()) {
  47. System.out.println("您录入的是文件路径,请输入一个文件夹路径:");
  48. }else {
  49. //5,将文件夹路径对象返回
  50. return dir;
  51. }
  52. }
  53. }
  54. /*
  55. * 统计该文件夹大小
  56. * 1,返回值类型long
  57. * 2,参数列表File dir
  58. */
  59. public static long getFileLength(File dir) {//dir = F:\day06\day07
  60. //1,定义一个求和变量
  61. long len = 0;
  62. //2,获取该文件夹下所有的文件和文件夹listFiles();
  63. File[] subFiles = dir.listFiles();//day07 Demo1_Student.class Demo1_Student.java
  64. //3,遍历数组
  65. for (File subFile : subFiles) {
  66. //4,判断是文件就计算大小并累加
  67. if(subFile.isFile()) {
  68. len = len + subFile.length();
  69. //5,判断是文件夹,递归调用
  70. }else {
  71. len = len + getFileLength(subFile);
  72. }
  73. }
  74. return len;
  75. }
  76. }

3、从键盘接收一个文件夹路径,删除该文件夹

  1. package com.heima.test;
  2. import java.io.File;
  3. public class Test2 {
  4. /**
  5. * 需求:2,从键盘接收一个文件夹路径,删除该文件夹
  6. *
  7. * 删除该文件夹
  8. * 分析:
  9. * 1,获取该文件夹下的所有的文件和文件夹
  10. * 2,遍历数组
  11. * 3,判断是文件直接删除
  12. * 4,如果是文件夹,递归调用
  13. * 5,循环结束后,把空文件夹删掉
  14. */
  15. public static void main(String[] args) {
  16. File dir = Test1.getDir();//获取文件夹路径
  17. deleteFile(dir);
  18. }
  19. /*
  20. * 删除该文件夹
  21. * 1,返回值类型 void
  22. * 2,参数列表File dir
  23. */
  24. public static void deleteFile(File dir) {
  25. //1,获取该文件夹下的所有的文件和文件夹
  26. File[] subFiles = dir.listFiles();
  27. //2,遍历数组
  28. for (File subFile : subFiles) {
  29. //3,判断是文件直接删除
  30. if(subFile.isFile()) {
  31. subFile.delete();
  32. //4,如果是文件夹,递归调用
  33. }else {
  34. deleteFile(subFile);
  35. }
  36. }
  37. //5,循环结束后,把空文件夹删掉
  38. dir.delete();
  39. }
  40. }

4、从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中

  1. package com.heima.test;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. public class Test3 {
  10. /**
  11. * 需求:3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
  12. *
  13. * 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
  14. * 分析:
  15. * 1,在目标文件夹中创建原文件夹
  16. * 2,获取原文件夹中所有的文件和文件夹,存储在File数组中
  17. * 3,遍历数组
  18. * 4,如果是文件就用io流读写
  19. * 5,如果是文件夹就递归调用
  20. * @throws IOException
  21. */
  22. public static void main(String[] args) throws IOException {
  23. File src = Test1.getDir();
  24. File dest = Test1.getDir();
  25. if(src.equals(dest)) {
  26. System.out.println("目标文件夹是源文件夹的子文件夹");
  27. }else {
  28. copy(src,dest);
  29. }
  30. }
  31. /*
  32. * 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
  33. * 1,返回值类型void
  34. * 2,参数列表File src,File dest
  35. */
  36. public static void copy(File src, File dest) throws IOException {
  37. //1,在目标文件夹中创建原文件夹
  38. File newDir = new File(dest, src.getName());
  39. newDir.mkdir();
  40. //2,获取原文件夹中所有的文件和文件夹,存储在File数组中
  41. File[] subFiles = src.listFiles();
  42. //3,遍历数组
  43. for (File subFile : subFiles) {
  44. //4,如果是文件就用io流读写
  45. if(subFile.isFile()) {
  46. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
  47. BufferedOutputStream bos =
  48. new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName())));
  49. int b;
  50. while((b = bis.read()) != -1) {
  51. bos.write(b);
  52. }
  53. bis.close();
  54. bos.close();
  55. //5,如果是文件夹就递归调用
  56. }else {
  57. copy(subFile,newDir);
  58. }
  59. }
  60. }
  61. }

5、从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印

  1. package com.heima.test;
  2. import java.io.File;
  3. public class Test4 {
  4. /**
  5. * 需求:4,从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印, 例如:
  6. * 把文件夹中的所有文件以及文件夹的名字按层级打印
  7. * 分析:
  8. * 1,获取所有文件和文件夹,返回的File数组
  9. * 2,遍历数组
  10. * 3,无论是文件还是文件夹,都需要直接打印
  11. * 4,如果是文件夹,递归调用
  12. * day07
  13. * day08
  14. * xxx.jpg
  15. * yyy.txt
  16. * Demo1_Consturctor.class
  17. * Demo1_Consturctor.java
  18. * Demo1_Student.class
  19. * Demo1_Student.java
  20. */
  21. public static void main(String[] args) {
  22. File dir = Test1.getDir();//获取文件夹路径
  23. printLev(dir,0);
  24. }
  25. public static void printLev(File dir,int lev) {
  26. //1,把文件夹中的所有文件以及文件夹的名字按层级打印
  27. File[] subFiles = dir.listFiles();
  28. //2,遍历数组
  29. for (File subFile : subFiles) {
  30. for(int i = 0; i <= lev; i++) {
  31. System.out.print("\t");
  32. }
  33. //3,无论是文件还是文件夹,都需要直接打印
  34. System.out.println(subFile);
  35. //4,如果是文件夹,递归调用
  36. if(subFile.isDirectory()) {
  37. //printLev(subFile,lev + 1);
  38. printLev(subFile,++lev);
  39. }
  40. }
  41. }
  42. }

方法2

  1. import java.io.File;
  2. /**
  3. * 打印层级目录
  4. * @author haoyongliang
  5. *
  6. */
  7. public class Demo5 {
  8. public static void main(String[] args) {
  9. show("E:\\JackDB-SQL-Database-Client_v3.0.0",0);
  10. }
  11. public static void show(String fileDir, int level){
  12. File file = new File(fileDir);
  13. //1、打印空格
  14. for(int i=0; i<level; i++){
  15. System.out.print("\t");
  16. }
  17. //2、打印文件或者文件夹名
  18. System.out.println(file.getAbsolutePath());
  19. //3、如果要是文件夹,继续递归
  20. if(file.isDirectory()){
  21. File[] listFiles = file.listFiles();
  22. if(listFiles != null){
  23. for(File f : listFiles){
  24. show(f.getAbsolutePath(),level+1);
  25. }
  26. }
  27. }
  28. }
  29. }

6、斐波那契-不死神兔

  1. package com.heima.test;
  2. public class Test5 {
  3. /**
  4. * * 不死神兔
  5. * 故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。
  6. * 在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡,
  7. * 问:一对刚出生的兔子,一年内繁殖成多少对兔子?
  8. * 1 1 2 3 5 8 13 21
  9. * 1 = fun(1)
  10. * 1 = fun(2)
  11. * 2 = fun(1) + fun(2)
  12. * 3 = fun(2) + fun(3)
  13. */
  14. public static void main(String[] args) {
  15. //demo1();
  16. System.out.println(fun(8));
  17. }
  18. public static void demo1() {
  19. //用数组做不死神兔
  20. int[] arr = new int[8];
  21. //数组中第一个元素和第二个元素都为1
  22. arr[0] = 1;
  23. arr[1] = 1;
  24. //遍历数组对其他元素赋值
  25. for(int i = 2; i < arr.length; i++) {
  26. arr[i] = arr[i - 2] + arr[i - 1];
  27. }
  28. //如何获取最后一个数
  29. System.out.println(arr[arr.length - 1]);
  30. }
  31. /*
  32. * 用递归求斐波那契数列
  33. */
  34. public static int fun(int num) {
  35. if(num == 1 || num == 2) {
  36. return 1;
  37. }else {
  38. return fun(num - 2) + fun(num - 1);
  39. }
  40. }
  41. }

7、求出1000的阶乘所有零和尾部零的个数,不用递归做

  1. package com.heima.test;
  2. import java.math.BigInteger;
  3. public class Test6 {
  4. /**
  5. * @param args
  6. * 需求:求出1000的阶乘所有零和尾部零的个数,不用递归做
  7. */
  8. public static void main(String[] args) {
  9. /*int result = 1;
  10. for(int i = 1; i <= 1000; i++) {
  11. result = result * i;
  12. }
  13. System.out.println(result);//因为1000的阶乘远远超出了int的取值范围
  14. */
  15. //demo1();
  16. demo2();
  17. }
  18. public static void demo2() {//获取1000的阶乘尾部有多少个零
  19. BigInteger bi1 = new BigInteger("1");
  20. for(int i = 1; i <= 1000; i++) {
  21. BigInteger bi2 = new BigInteger(i+"");
  22. bi1 = bi1.multiply(bi2);//将bi1与bi2相乘的结果赋值给bi1
  23. }
  24. String str = bi1.toString();//获取字符串表现形式
  25. StringBuilder sb = new StringBuilder(str);
  26. str = sb.reverse().toString();//链式编程
  27. int count = 0;//定义计数器
  28. for(int i = 0; i < str.length(); i++) {
  29. if('0' != str.charAt(i)) {
  30. break;
  31. }else {
  32. count++;
  33. }
  34. }
  35. System.out.println(count);
  36. }
  37. public static void demo1() {//求1000的阶乘中所有的零
  38. BigInteger bi1 = new BigInteger("1");
  39. for(int i = 1; i <= 1000; i++) {
  40. BigInteger bi2 = new BigInteger(i+"");
  41. bi1 = bi1.multiply(bi2);//将bi1与bi2相乘的结果赋值给bi1
  42. }
  43. String str = bi1.toString();//获取字符串表现形式
  44. int count = 0;
  45. for(int i = 0; i < str.length(); i++) {
  46. if('0' == str.charAt(i)) {//如果字符串中出现了0字符
  47. count++;//计数器加1
  48. }
  49. }
  50. System.out.println(count);
  51. }
  52. }

8、约瑟夫环

  1. package com.heima.test;
  2. import java.util.ArrayList;
  3. public class Test8 {
  4. /**
  5. * @param args
  6. * 约瑟夫环
  7. * * 幸运数字
  8. */
  9. public static void main(String[] args) {
  10. System.out.println(getLucklyNum(8));
  11. }
  12. /*
  13. * 获取幸运数字
  14. * 1,返回值类型int
  15. * 2,参数列表int num
  16. */
  17. public static int getLucklyNum(int num) {
  18. ArrayList<Integer> list = new ArrayList<>();//创建集合存储1到num的对象
  19. for(int i = 1; i <= num; i++) {
  20. list.add(i);//将1到num存储在集合中
  21. }
  22. int count = 1;//用来数数的,只要是3的倍数就杀人
  23. for(int i = 0; list.size() != 1; i++) {//只要集合中人数超过1,就要不断的杀
  24. if(i == list.size()) {//如果i增长到集合最大的索引+1时
  25. i = 0;//重新归零
  26. }
  27. if(count % 3 == 0) {//如果是3的倍数
  28. list.remove(i--);//就杀人
  29. }
  30. count++;
  31. }
  32. return list.get(0);
  33. }
  34. }

9、今天必须掌握的内容,面试题,笔试题。(掌握这个就可以放心学习后面的知识了)

1、所有代码必须掌握2、编写一个程序,把指定目录下的所有文件拷贝到另一个目录中, 拷贝成功后,把后缀名是.java的改成.txt

来自为知笔记(Wiz)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857