随机数生成器

一、随机数生成器:

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

源代码

package org.yuan.Day3;
import java.util.Scanner;
public class RandomNumber {
static Scanner sc=new Scanner(System.in);
    
    static public void Random(long seed)
    {
        long n,x1;
        System.out.println("请输入想要产生的随机数的个数:");
        n=sc.nextLong();
        for(long i=1;i<=n;i++)
        {
            x1=(16807*seed)%(Integer.MAX_VALUE);
            System.out.println(x1);
            seed=x1;
        }
        
    }
    public static void main(String []args)
    {
        long a;
        a=(long)(Math.random()*100);
        Random(a);
    }

}

二、方法重载

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

}

这个在运行过程中,会根据参数类型的不同而执行不同的方法。这里square方法的名字相同,但是传参类型不同,这就是方法的重载。

重载的方法中:参数的类型不同;参数的个数不同;或者是参数类型的顺序不同。这三者必须满足其中一个条件。

原文地址:https://www.cnblogs.com/tianwenjing123-456/p/11600863.html