动手动脑2

动手动脑1:

生成随机数算法编写

package session;

 

import java.util.Scanner;

 

public class Random {

    public void random() {

        int n;

        Scanner sc=new Scanner(System.in);

        long seed=System.currentTimeMillis();

        int multiplier=16807;

        int c=0,i;

        long random=(multiplier*seed+c)%Integer.MAX_VALUE;

        System.out.println("请输入生成随机数的数目:");

        n=sc.nextInt();

        for(i=0;i<n;i++) {

            System.out.print(random+" ");

            random=(multiplier*random+c)%Integer.MAX_VALUE;

            if(i%10==0&&i!=0)System.out.println();

        }

    }

    public static void main(String[] args) {

        Random r=new Random();

        r.random();

    }

}

 运行结果:

动手动脑2:

package session;

//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("\nThe 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;

    }

}

以上代码中使用了方法重载,而构成重载需要满足以下条件:

(1)      方法名相同。

(2)      参数类型不同、参数个数不同、参数类型顺序不同(是参数类型不同)。

注意点:方法的返回值不能作为重载的判断方法。因为在使用同名的方法时时,调用前需要先判断使用哪个方法,而返回值是在调用方法后才会返回,因而返回值不作为重载的判断条件。

原文地址:https://www.cnblogs.com/chenghaixiang/p/14158452.html