动手动脑

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

import java.util.Scanner;
public class RollDie {
public static void main( String args[] )
{

double n=1;
Scanner scanner = new Scanner(System.in);
System.out.println("随机数的个数:");
int m = scanner.nextInt();
for(int i=1;i<=m;i++)
{
n=16807*n%(2^1000000-2);
System.out.println("第"+i+"个随机数:"+n);
}
}
}

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

 代码的两个方法名相同,返回值与参数类型不同,这是方法重载。

posted @ 2017-10-13 16:48  劫殇  阅读(71)  评论(0编辑  收藏  举报