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 }

结果截图:

时间: 2024-10-13 22:43:41

Java课堂动手动脑--方法的相关文章

Java课堂动手动脑-截图集锦

课堂实践性问题 没有数据类型,名称与类名相同,没有返回值 类本身有自定义的构造方法,调用时应有参数,这时系统不再使用默认构造方法 类字段初始化顺序 1.执行类成员定义时指定的默认值或累的初始化块,执行哪一个看哪一个排在前面. 2.执行类的构造函数 动手动脑问题(类之间继承,创建子类对象导致父类初始化块的执行) 静态初始化执行顺序 1.静态初始化块只执行一次 2.创建子类的对象时,父类的初始化块也会执行 静态方法访问实例成员变量如下:

java课堂动手动脑

实验任务一:阅读并运行示例PassArray.java. 1)源代码: package demo; //PassArray.java //Passing arrays and individual array elements to methods public class PassArray { public static void main(String[] args) { int a[] = { 1, 2, 3, 4, 5 }; String output = "The values of

Java课堂 动手动脑5

1.了解棋盘打印:利用二维数组输出一个15*15的棋盘,棋盘的原素为"+",就是输出一个横纵都是15个"+"的二维数组,然后再以坐标形式输入棋子"●",替换掉原来棋盘里的"+".再编写代码. 电脑随机生成2个整数,作为电脑下棋的座标,赋给board数组.还涉及1.座标的有效性,只能是数字,不能超出棋盘范围2.如果下的棋的点,不能重复下棋.3.每次下棋后,需要扫描谁赢了 代码如下: import java.io.*; publi

Java课堂 动手动脑6

一.下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么? m=d;d=m;d=(Dog)m;d=c;c=(Cat)m; 先进行自我判断, 1.代码: class Mammal{} class Dog extends Mammal {} class Cat extends Mammal{} public class TestCast { public static void main(String args[]) { Mammal m; Dog d=new Dog(); Cat

java课堂 动手动脑3

(1) 该函数没有赋初值再就是如果类提供一个自定义的构造方法,将导致系统不在提供默认的构造方法. (2) public class test { public static void main(String[] args) { // TODO Auto-generated method stub InitializeBlockClass obj=new InitializeBlockClass(); System.out.println(obj.field); obj=new Initializ

JAVA课堂-动手动脑1

一.Enum:一般用来表示一组相同类型的常量.对这些属性用常量的好处是显而易见的,不仅可以保证单例,且比较时候可以用”==”来替换equals,枚举对象里面的值都必须是唯一的. 代码: public class EnumTest {      public static void main(String[] args) { Size s=Size.SMALL; Size t=Size.LARGE;//s和t引用同一个对象? System.out.println(s==t);  ////是原始数据

java课堂动手动脑总结

java有8种基本数据类型:byte,int,short,long,boolean,char,float,double. 对应的为:Byte,Int,Short,Long,Boolean,Charecter,Float,Double.其中boolean是逻辑型,char是文本型,byte,short,int,long是整数型,float,double是浮点型. byte:1字节 -128~127      short:2字节  -2*15~2*15-1    int:4字节   -2*31  ~

java多态动手动脑

实验任务一:多态实现ATM工作原理 1)源代码: package demo; import java.util.Scanner; class A{ String name; String date; String mima; int yu; String kahao; public A(String name,String date,String mima,int yu,String kahao) { this.name=name; this.date=date; this.mima=mima;

Java的动手动脑

动手动脑及课后实 仔细阅读示例: EnumTest.java,运行它,分析运行结果? public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Size t=Size.LARGE; //s和t引用同一个对象? System.out.println(s==t);  // //是原始数据类型吗? System.out.println(s.getClass().isPrimitive());