zoukankan      html  css  js  c++  java
  • Java课堂动手动脑--方法

    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 ('
    ').
     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 }

    结果截图:

  • 相关阅读:
    Yii2 分页
    Yii2 或者当前登录用户帐号
    css3媒体查询判断移动设备横竖屏
    Javascript操作Tr隐藏显示变形~
    php注释标准
    匹配一段html中所有的src
    数据库遇到错误(随时补充)
    NetCore-缓存文件上传和文件流上传
    SVN跨服务器版本迁移
    发票同步微信卡包
  • 原文地址:https://www.cnblogs.com/liuxining/p/5966463.html
Copyright © 2011-2022 走看看