Java课堂动手动脑--方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java课堂动手动脑--方法相关的知识,希望对你有一定的参考价值。

1、查看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.

 

2、三种方法求组合数
(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 }

 

结果截图:

技术分享

技术分享

技术分享

技术分享

技术分享


以上是关于Java课堂动手动脑--方法的主要内容,如果未能解决你的问题,请参考以下文章

Java动手动脑第四讲课堂作业

java课堂动手动脑

Java课堂动手动脑

java课堂动手动脑

JAVA课堂练习-动手动脑--数组

Java课堂 动手动脑6