1.编写一个方法,使用以上算法生成指定数目的随机数
package 测试;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("输入你要获取的个数:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//获取的随机数的个数
for(int i = 0;i < n;i++)//for循环
System.out.println((int)(Math.random()*1000));//输出获取的1-1000的随机数
}
}
2.请看以下代码,你发现了有什么特殊之处吗?
public class MethodOverload {
public static void main(String[] args) {
// TODO Auto-generated method stub
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;
}
}
函数的重载,它们的方法名相等,但是他们的参数类型不同,所以调用的函数不同,所以结果不同。