03 方法

【动手动脑】

当JAVA里定义的函数中去掉static后,怎么办?(如下程序,将square()函数的static去掉)

复制代码
public class SquareIntTest {

    public static void main(String[] args) {
        int result;

        for (int x = 1; x <= 10; x++) {
            result = square(x);
            // Math库中也提供了求平方数的方法
             //result=(int)Math.pow(x,2);
            System.out.println("The square of " + x + " is " + result + "
");
        }
    }

    // 自定义求平方数的静态方法
    //问:如果不加static,怎么办?
    public static int square(int y) {
        return y * y;
    }
}
复制代码

因为static 是静态的意思,main是静态的,想加载的时候就加载了,可将函数写在一个新的类中即可,如下所示:

复制代码
public class SquareIntTest {

    public static void main(String[] args) {
        int result;

        for (int x = 1; x <= 10; x++) {
            result = new Test().square1(x);//非静态
            System.out.println("The square of " + x + " is " + result + "
");
        }
    }
}
class Test{
    public int square1(int y) {
        return y*y;
       
  }
}
复制代码

【动手动脑】

编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。

import java.util.Scanner;

public class Random {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入希望产生的随机数的个数:");
Scanner input=new Scanner(System.in);
int N=input.nextInt();
int result;
for(int i=0;i<N;i++)
{
result=(int)(Math.random()*10000);
System.out.println(result);
}
}
}

【动手动脑】

请看以下代码,你发现了有什么特殊之处吗?

// MethodOverload.java
// Using overloaded methods

public class MethodOverload {

public static void main(String[] args) {
System.out.println("The square of integer 7 is " + square(7));
System.out.println(" The square of double 7.5 is " + square(7.5));
}

public static int square(int x) {
return x * x;
}

public static double square(double y) {
return y * y;
}
}

答:此代码中采用static调用自定义方法,如果不加static可以使用类名.成员名或者对象名.成员名调用。

三、递归

1、编个程序求n!

import java.math.BigInteger;
import java.util.Scanner;


public class CalculateN {


public static void main(String[] args) {
System.out.print("请输入N:");
Scanner scanner=new Scanner(System.in);
int number=scanner.nextInt();
System.out.println(number+"!="+calculateN2(number));

}

public static long calculateN(int n) {
if(n==1 || n==0){
return 1;
}

return n*calculateN(n-1);
}

public static BigInteger calculateN2(int n) {
if(n==1 || n==0){
return BigInteger.valueOf(1);
}
return BigInteger.valueOf(n).multiply(calculateN2((n-1)));
}
}

四、处理大数字和浮点数

2、看上面程序中出现的错误,阶乘数怎么可能出现复数?

原因:java中int类型的数值占32位,是有符号的,long类型的数值占64位。由于计算机使用固定的位数来保存数值,因此,能处理的数值大小是有限的,当要处理的数值超过了这一范围时,计算机将会自动截断数值的二进制表示为它所能处理的最多位数,这将导致错误的处理结果。

五、课后作业

背景:杨辉三角形与组合数公式

利用杨辉三角形原理来计算 组合数

使用计算机计算组合数:

(1)使用组合数公式利用n!来计算

import java.math.BigInteger;
import java.util.Scanner;

public class YangHui {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("求组合数C(n,k):(n为下标)");//用户输入
System.out.print("请输入n,k:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
if(n<k)
System.out.println("ERROR!");
else if(n==0&&k==0)
System.out.println("1");
else
System.out.print("C("+n+","+k+")="+Calculate(n)/Calculate(k)/Calculate(n-k));
}
public static long Calculate(int n) {
if(n==1)
{
return 1;
}

return n*Calculate(n-1);
}
}

 

(2)使用递推的方法用杨辉三角形计算

 

import java.math.BigInteger;
import java.util.Scanner;

public class YangHui {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("求组合数C(n,k):(n为下标)");//用户输入
System.out.print("请输入n,k:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
if(n<k)
System.out.println("ERROR!");
else if(n==0&&k==0)
System.out.println("1");
else
System.out.print("C("+n+","+k+")="+Calculate(n)/Calculate(k)/Calculate(n-k));
}
public static long Calculate(int n) {
if(n==1)
{
return 1;
}

return n*Calculate(n-1);
}
}

(3)使用递归的方法用组合数递推公式计算

 

 课后作业2 

递归编程解决汉诺塔问题。用Java实现

课后作业3

使用递归方式判断某个字串是否是回文( palindrome )

public class Huiwen {

    public static boolean isHuiwen(String s,int i,int j)  

   {   if(i>j)    throw new IllegalArgumentException();   

       if(i == j)    return true;   

       else{    return (s.charAt(i)==s.charAt(j))&& isHuiwen(s,i+1,j-1);   }

}  

    public static void main(String[] args) {

      String test = "ABCBA";

     int i=0;   int j=test.length()-1;

     System.out.println(test + " is Palindrome?" + Huiwen.isHuiwen(test, i, j));

 }  

}

原文地址:https://www.cnblogs.com/610553824lyx/p/5966205.html