1.重载函数
代码:
1 // MethodOverload.java 2 // Using overloaded methods 3 4 public class MethodOverload { 5 6 public static void main(String[] args) { 7 System.out.println("The square of integer 7 is " + square(7)); 8 System.out.println("\nThe square of double 7.5 is " + square(7.5)); 9 } 10 11 public static int square(int x) { 12 return x * x; 13 } 14 15 public static double square(double y) { 16 return y * y; 17 } 18 }
程序思想:用重载函数实现不同类型的数据类型的平方计算。
程序收获:重载函数有三种重载方式:1.形参类型重载 2.形参个数重载 3.形参顺序重载。
2.纯随机数生成器
代码:
1 package Ran; 2 3 4 5 import java.util.Random; 6 import java.util.Scanner; 7 8 public class Ran { 9 public static void main(String args[]) 10 { 11 Random sr=new Random(); 12 System.out.println("请输入你要生成的随机数数目:"); 13 Scanner sc=new Scanner(System.in); 14 int a=sc.nextInt(); 15 for(int i=0;i<a;i++) 16 { 17 int b=sr.nextInt(100); 18 b=(16807*b+0)%2147483647; 19 System.out.println(b); 20 } 21 } 22 23 24 }
程序思想:利用随机数的种子生成方法生成纯随机数。具体方法是x=(a*x+c)%m;