动手动脑

动手动脑1、

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

import java.util.Random;
import java.util.Scanner;
public class TestSeed
{
    public static void main(String[] args)
 {
       
        Random r = new Random(System.currentTimeMillis());
        System.out.println("请输入要得到多少随机数");
        int m;
        Scanner in = new Scanner(System.in);
        m = in.nextInt();
        for(int i=0;i<m;i++)
         System.out.println( r.nextInt());
        
    }
}
运行结果:
请输入要得到多少随机数
10
1620020807
-2070122496
58291264
742117536
-1983758661
-1649711892
1181536659
1091375721
-870160946
-1878357734

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

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;
         }
}
运行结果:
The square of integer 7 is 49

The square of double 7.5 is 56.25
 

满足以下条件的两个或多个方法构成重载关系:

(1)方法名相同

(2)参数类型不同,参数个数不同,或者是参数类型的顺序不同。

原文地址:https://www.cnblogs.com/MoooJL/p/11610497.html