zoukankan      html  css  js  c++  java
  • 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类型 ,但是可以打印出其他数据类型。

  • 相关阅读:
    创建型模式
    建造者模式
    抽象工厂模式
    工厂方法模式
    原型模式
    [水]三个日常签到题
    [数]青蛙的约会&Strange function
    [水]三个数学的小技巧题
    Jquery Ajax的使用
    easyui学习记录:combotree的使用
  • 原文地址:https://www.cnblogs.com/ssyh/p/7661198.html
Copyright © 2011-2022 走看看