java动手动脑

随机数算法

设计思想:

利用生产随机数的数学公式:  

a,c,m为参数,还需要一个种子参数X0

源代码:

import java.util.Scanner;

public class Test

{

public static void main(String args[])

{

Scanner input=new Scanner(System.in);

int m;

System.out.println("输入要生产的随机数的数量:");

m=input.nextInt();

System.out.println("随机的结果为:");

random(m);

}

public static void random(int n)

{

int a,c,x0,m;

long x;

a=75;

c=0;

m=(int) (Math.pow(2, 33)-1);

x0=156;

x=x0;

for(int i=0;i<n;i++)

{

x=(a*x+c)%m;

System.out.print(x+" ");

if(i%10==0)

System.out.println();

}

}

}

结果截图:

 

动手动脑二:


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

发现: 两个方法的方法名完全一致,但是参数类型不同,调用这两个方法时,会自动根据实参的类型正确调用方法,不会出现混淆。

练习:

查看关于JDK中的System.out.println()方法:

方法的参数是String类型 ,但是可以打印出其他数据类型。

原文地址:https://www.cnblogs.com/ssyh/p/7661198.html