zoukankan      html  css  js  c++  java
  • java课堂 动手动脑2

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

    Modulus=231-1=int.MaxValue

    Multiplier=75=16807

    C=0

    当显示过231-2个数之后,才可能重复。

    public class suiji

    {

      private static final int N = 200;

      private static final int LEFT = 40;

      private static final int RIGHT = 10000;

      private static long x0 = 1L;

      private long a = 1103515245L;

      private long b= 12345L;

      private long c = 2147483648L;

     

      // 产生随机数

      private long rand ( long r )

      {

        // a,b,c为常数

        r = ( r * a + b ) % m;//Xn+1=(aXn + b)mod m

        return r;

      }

     

      private long little ( int a, int v, long rand )

      {

        return a + rand % ( v - a + 1 );

      }

     

      private void recursion ( int count, long rand )

      {

        if (count >= N)

        {

          return;

        }

        rand = rand (rand);

        long r = little (LEFT, RIGHT, rand);

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

        recursion (++count, rand);

      }

     

      public static void main ( String[] args )

      {

        suiji recur = new suiji ();

        recur.recursion (0, x0);

      }

    }

     

    2方法重载

    请看以下代码,你发现了有什么特殊之处吗?

     

    我发现同样名称的函数可以不同,这使得函数变得更全面,不同的参数类型可以自动调用相应的函数(方法),即方法的重载。

    满足以下条件的两个或多个方法构成重载关系:

    1)方法名相同;

    2)参数类型不同,参数个数不同,或者是参数类型的顺序不同。

    注意:方法的返回值不作为方法重载的判断条件。

  • 相关阅读:
    Python内置函数(55)——round
    Python内置函数(54)——reversed
    Python内置函数(53)——repr
    Python内置函数(52)——range
    Python内置函数(51)——property
    Python内置函数(50)——print
    Python内置函数(49)——pow
    Python内置函数(48)——ord
    Python内置函数(47)——open
    Python内置函数(46)——oct
  • 原文地址:https://www.cnblogs.com/conquer-vv/p/5966104.html
Copyright © 2011-2022 走看看