首页 > 其他 > 详细

01-方法 动手动脑

时间:2018-10-14 18:32:59      阅读:112      评论:0      收藏:0      [点我收藏+]

编写一个方法,使用以上算法生成指定数目(比如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;
    }
}

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

 

01-方法 动手动脑

原文:https://www.cnblogs.com/gothic-death/p/9787177.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!