天天看點

java關于随機數和方法重構

1.生成随機數

源代碼

package Zuote;

public class SuiJiShu {

    public static void main( String args[] )

    {

       java.util.Random r=new java.util.Random();

       for(int i=0;i<1000;i++)

       {

           System.out.println(r.nextInt(10)+1);

        }

    }

}

結果截圖

java關于随機數和方法重構

2.以下代碼的特殊之處

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;

該代碼特殊在函數的重載,不同類型相同函數名,當實參為int類型時調用int類型的函數,當實參為double類型時調用double類型的函數