首页 > 技术文章 > 01-方法 动手动脑

gothic-death 2018-10-14 17:54 原文

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

 

import java.util.*;

public class Random {
    public static void main( String [] args ){
        Random rand = new Random();
        Scanner in = new Scanner( System.in );
        int number;
        System.out.println("请输入生成的随机数数量");
        number = in.nextInt();
        for( int i=0; i<number; i++){
            System.out.println(rand.nextInt());
        }
    }
}

 

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

// 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;
    }
}

参数类型不同可以构成“重载”关系

 

推荐阅读