博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java课堂动手动脑--方法
阅读量:6787 次
发布时间:2019-06-26

本文共 4263 字,大约阅读时间需要 14 分钟。

1、方法重载

 

方法重载,调用某个方法,如果有多个方法方法名都是这个方法名,则根据实参的类型、个数、顺序进行匹配。 2、查看JDK文档 System.out.println方法重载
1 println 2 public void println() 3 Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n'). 4  5 public void println(boolean x) 6 Prints a boolean and then terminate the line. This method behaves as though it invokes print(boolean) and then println(). 7 Parameters: 8 x - The boolean to be printed 9 10 public void println(char x) 11 Prints a character and then terminate the line. This method behaves as though it invokes print(char) and then println(). 12 Parameters: 13 x - The char to be printed. 14 15 public void println(int x) 16 Prints an integer and then terminate the line. This method behaves as though it invokes print(int) and then println(). 17 Parameters: 18 x - The int to be printed. 19 20 public void println(long x) 21 Prints a long and then terminate the line. This method behaves as though it invokes print(long) and then println(). 22 Parameters: 23 x - a The long to be printed. 24 25 public void println(float x) 26 Prints a float and then terminate the line. This method behaves as though it invokes print(float) and then println(). 27 Parameters: 28 x - The float to be printed. 29 30 public void println(double x) 31 Prints a double and then terminate the line. This method behaves as though it invokes print(double) and then println(). 32 Parameters: 33 x - The double to be printed. 34 35 public void println(char[] x) 36 Prints an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println(). 37 Parameters: 38 x - an array of chars to print. 39 40 public void println(String x) 41 Prints a String and then terminate the line. This method behaves as though it invokes print(String) and then println(). 42 Parameters: 43 x - The String to be printed. 44 45 public void println(Object x) 46 Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println(). 47 Parameters: 48 x - The Object to be printed.

 

3、三种方法求组合数 (1)使用组合数公式

源代码

1 //利用组合数公式采用阶乘计算组合数 2  3 package boKeYuan; 4  5 import java.util.Scanner; 6  7 public class ZuHeShu { 8  9     public static void main(String[] args) {10         int n,k,sum;11         Scanner scan = new Scanner(System.in); 12 13 System.out.println("请输入总数和取出的个数:"); 14 15 n = scan.nextInt(); 16 k = scan.nextInt(); 17 18 sum = jieCheng(n) / (jieCheng(k) * jieCheng(n - k)); 19 20 System.out.println("从" + n + "个中取出" + k + "个共有 " + sum + " 种组合"); 21  scan.close(); 22 23  } 24 25 //递归计算n的阶乘 26 public static int jieCheng(int n){ 27 if(n < 0) //n值不合法 28 return 0; 29 if(n == 0 || n == 1) 30 return 1; 31 else 32 return n * jieCheng(n - 1); 33  } 34 35 }

(2)利用杨辉三角原理递推求组合数

原理:

源代码:

1 //利用杨辉三角原理递推求组合数 2  3  4 import java.util.Scanner; 5  6 public class ZuHe03 { 7     public static void main(String[] args) { 8         int i,j,n,k; 9         10         Scanner scan = new Scanner(System.in);11         12         System.out.println("请输入总数和取出的个数:"); 13 14 n = scan.nextInt(); 15 k = scan.nextInt(); 16 int[][] y = new int[n + 1][n + 1]; 17 y[0][0] = 1; 18 for(i = 1;i <= n;i++){ 19 y[i][0] = y[i][i] = 1; 20 for(j = 1;j < i;j++) 21 y[i][j] = y[i - 1][j - 1] + y[i - 1][j]; 22  } 23 24 System.out.println("从" + n + "个中取出" + k + "个共有 " + y[n][k] + " 种组合"); 25  scan.close(); 26  } 27 }

(3)利用杨辉三角原理递归求组合数

1 //计算组合数,利用杨辉三角原理递归实现 2  3 import java.util.Scanner; 4  5 public class ZuHeShu2 { 6  7     public static void main(String[] args) { 8         int n,k,sum; 9         Scanner scan = new Scanner(System.in);10         11         System.out.println("请输入总数和取出的个数:"); 12 13 n = scan.nextInt(); 14 k = scan.nextInt(); 15 16 sum = zuHe(n,k); 17 18 System.out.println("从" + n + "个中取出" + k + "个共有 " + sum + " 种组合"); 19  scan.close(); 20  } 21 22 public static int zuHe(int n,int k){ 23 if(k < 0 || n < k) 24 return 0; 25 if(n == 0 || n == 1){ 26 return 1; 27  } 28 else 29 return zuHe(n - 1,k - 1) + zuHe(n - 1,k); 30  } 31 }

 

结果截图:

转载于:https://www.cnblogs.com/liuxining/p/5966463.html

你可能感兴趣的文章
centOS 6.2 升级 kernel 3.2.9
查看>>
jenkins构建shell或者python脚本中含有远程登录复制会报错解决办法
查看>>
python脚本 字符串变量 强制 不转义 win地址 不转义输出
查看>>
IT自学要走远,更要走深
查看>>
Java中常用的几种排序算法
查看>>
分支3-CentOS6.5下 子域授权、请求转发 的教程
查看>>
Javascript 拖拽 放大镜
查看>>
利用PowerShell创建事件日志
查看>>
python光荣之路测试开发班list学习笔记
查看>>
【c#】Excel COM组件在.net程序中的使用
查看>>
python的一些细节(持续更新中)
查看>>
requests中 .text 和 .content区别
查看>>
自定义 java 加载器
查看>>
JavaScript删除数组重复元素的5个高效算法
查看>>
【SVN】setup SVN Server
查看>>
Powershell 提示错误:get-help about_signing 解决
查看>>
cisco 和 华为的设备如何设置命令不分页显示
查看>>
nethogs监控进程网络流量
查看>>
Win7下chm文件无法打开问题解决方法
查看>>
DDOS***类型以及iptables防范ddos脚本
查看>>