zoukankan      html  css  js  c++  java
  • 2016 10月15日java的动手动脑

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

          源程序:

          

     //随机数的产生

    //zhanxinwu,October,15,2016

    public class Recur

    {

     private static final int N = 10;

     private static final int LEFT = 40;

     private static final int RIGHT = 100;

     private static long x0 = 1L;

     private long a = 1103515245L;

     private long c = 12345L;

     private long m = 2147483648L;

     // 产生随机数

     private long rand ( long r )

     {

      // a,c,m为常数

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

      return r;

     }

      * @param a

      * @param b

      * @param rand

      * @return

      */

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

     {

      return a + rand % ( b - 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 )

     {

      Recur recur = new Recur ();

      recur.recursion (0, x0);

     }

    改程序就调用了Xn+1 =(aXn+c)mod m;的公式进行编辑

       

     public class MethodOver load {

             public static void main (String[] args){

                 System.out.println(“The sqare of integer 7 is” +square(7));

                 System.out.println(“The sqare of double 7.5 is” +square(7));

     }

     Public static int square(int x){

        Return x*x;

    }

      Public static double square(double y){

        Return y*y;

    }

    }

    答:该程序上述示例代码展示了Java的“方法重载(overload)”特性。

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

    (1)方法名相同;

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

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

    答:改程序主要是输入一个数如何识别,调用哪个函数的类型,最终改程序可以判断的是输入值的类型,来确定调用函数是哪一个。 

  • 相关阅读:
    日志框架1---多种日志门面和日志实现初步了解(转载)
    @Autowired 与@Resource的区别(转)
    Spring 之jdbc模板(JdbcTemplate与NamedParameterJdbcTemplate)
    springMVC中的Controller里面定义全局变量
    spring批量更新数据 ---- BatchPreparedStatementSetter
    easyui树查找
    根据ID和parentID利用Java递归获取全路径名称
    一个统一将数据转换为JSON的方法
    上传图片并限制图片格式和大小
    js上传文件(图片)的格式和大小限制
  • 原文地址:https://www.cnblogs.com/janson666/p/5964942.html
Copyright © 2011-2022 走看看