加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

14-02 Java Math类,Random类,System类,BigDecimal类

发布时间:2020-12-14 06:09:02 所属栏目:Java 来源:网络整理
导读:Math类 Math的方法 span style="color: #008000;"/* span style="color: #008000;" Math:用于数学运算的类。 成员变量: public static final double PI public static final double E 成员方法: public static int abs(int a):绝对值 public static doubl

Math类

Math的方法

<span style="color: #008000;">/*<span style="color: #008000;">

  • Math:用于数学运算的类。

  • 成员变量:

  • public static final double PI

  • public static final double E

  • 成员方法:

  • public static int abs(int a):绝对值

  • public static double ceil(double a):向上取整

  • public static double floor(double a):向下取整

  • public static int max(int a,int b):最大值 (min自学)

  • public static double pow(double a,double b):a的b次幂

  • public static double random():随机数 [0.0,1.0)

  • public static int round(float a) 四舍五入(参数为double的自学)

  • public static double sqrt(double a):正平方根
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> MathDemo {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    <span style="color: #008000;">//<span style="color: #008000;"> public static final double PI
    System.out.println("PI:" +<span style="color: #000000;"> Math.PI);
    <span style="color: #008000;">//<span style="color: #008000;"> public static final double E
    System.out.println("E:" +<span style="color: #000000;"> Math.E);
    System.out.println("--------------"<span style="color: #000000;">);

     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; public static int abs(int a):绝对值</span>
     System.out.println("abs:" + Math.abs(10<span style="color: #000000;"&gt;));
     System.out.println(</span>"abs:" + Math.abs(-10<span style="color: #000000;"&gt;));
     System.out.println(</span>"--------------"<span style="color: #000000;"&gt;);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; public static double ceil(double a):向上取整</span>
     System.out.println("ceil:" + Math.ceil(12.34<span style="color: #000000;"&gt;));
     System.out.println(</span>"ceil:" + Math.ceil(12.56<span style="color: #000000;"&gt;));
     System.out.println(</span>"--------------"<span style="color: #000000;"&gt;);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; public static double floor(double a):向下取整</span>
     System.out.println("floor:" + Math.floor(12.34<span style="color: #000000;"&gt;));
     System.out.println(</span>"floor:" + Math.floor(12.56<span style="color: #000000;"&gt;));
     System.out.println(</span>"--------------"<span style="color: #000000;"&gt;);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; public static int max(int a,int b):最大值</span>
     System.out.println("max:" + Math.max(12,23<span style="color: #000000;"&gt;));
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 需求:我要获取三个数据中的最大值
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 方法的嵌套调用</span>
     System.out.println("max:" + Math.max(Math.max(12,23),18<span style="color: #000000;"&gt;));
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 需求:我要获取四个数据中的最大值</span>
     System.out.println("max:"
             + Math.max(Math.max(12,78),Math.max(34,56<span style="color: #000000;"&gt;)));
     System.out.println(</span>"--------------"<span style="color: #000000;"&gt;);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; public static double pow(double a,double b):a的b次幂</span>
     System.out.println("pow:" + Math.pow(2,3<span style="color: #000000;"&gt;));
     System.out.println(</span>"--------------"<span style="color: #000000;"&gt;);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; public static double random():随机数 [0.0,1.0)</span>
     System.out.println("random:" +<span style="color: #000000;"&gt; Math.random());
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取一个1-100之间的随机数</span>
     System.out.println("random:" + ((<span style="color: #0000ff;"&gt;int</span>) (Math.random() * 100) + 1<span style="color: #000000;"&gt;));
     System.out.println(</span>"--------------"<span style="color: #000000;"&gt;);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; public static int round(float a) 四舍五入(参数为double的自学)</span>
     System.out.println("round:" + Math.round(12.34f<span style="color: #000000;"&gt;));
     System.out.println(</span>"round:" + Math.round(12.56f<span style="color: #000000;"&gt;));
     System.out.println(</span>"--------------"<span style="color: #000000;"&gt;);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;public static double sqrt(double a):正平方根</span>
     System.out.println("sqrt:"+Math.sqrt(4<span style="color: #000000;"&gt;));

    }
    }

Math.random()

<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.Scanner;

<span style="color: #008000;">/*<span style="color: #008000;">

  • 需求:请设计一个方法,可以实现获取任意范围内的随机数。

  • 分析:

  • A:键盘录入两个数据。

  • int strat;

  • int end;

  • B:想办法获取在start到end之间的随机数

  • 我写一个功能实现这个效果,得到一个随机数。(int)

  • C:输出这个随机数
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> MathDemo {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    Scanner sc = <span style="color: #0000ff;">new<span style="color: #000000;"> Scanner(System.in);
    System.out.println("请输入开始数:"<span style="color: #000000;">);
    <span style="color: #0000ff;">int start =<span style="color: #000000;"> sc.nextInt();
    System.out.println("请输入结束数:"<span style="color: #000000;">);
    <span style="color: #0000ff;">int end =<span style="color: #000000;"> sc.nextInt();

     </span><span style="color: #0000ff;"&gt;for</span> (<span style="color: #0000ff;"&gt;int</span> x = 0; x < 100; x++<span style="color: #000000;"&gt;) {
         </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 调用功能</span>
         <span style="color: #0000ff;"&gt;int</span> num =<span style="color: #000000;"&gt; getRandom(start,end);
         </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 输出结果</span>

    <span style="color: #000000;"> System.out.println(num);
    }
    }

    <span style="color: #008000;">/*<span style="color: #008000;">

    • 写一个功能 两个明确: 返回值类型:int 参数列表:int start,int end
      <span style="color: #008000;">*/
      <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">int getRandom(<span style="color: #0000ff;">int start,<span style="color: #0000ff;">int<span style="color: #000000;"> end) {

      <span style="color: #0000ff;">int number = (<span style="color: #0000ff;">int) (Math.random() * (end - start + 1)) +<span style="color: #000000;"> start;
      <span style="color: #0000ff;">return<span style="color: #000000;"> number;
      }
      }

?

Random类

<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.Random;

<span style="color: #008000;">/*<span style="color: #008000;">

  • Random:产生随机数的类

  • 构造方法:

  • public Random():没有给种子,用的是默认种子,是当前时间的毫秒值

  • public Random(long seed):给出指定的种子

  • 给定种子后,每次得到的随机数是相同的。

  • 成员方法:

  • public int nextInt():返回的是int范围内的随机数

  • public int nextInt(int n):返回的是[0,n)范围的内随机数
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> RandomDemo {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    <span style="color: #008000;">//<span style="color: #008000;"> 创建对象
    <span style="color: #008000;">//<span style="color: #008000;"> Random r = new Random();
    Random r = <span style="color: #0000ff;">new Random(1111<span style="color: #000000;">);

     </span><span style="color: #0000ff;"&gt;for</span> (<span style="color: #0000ff;"&gt;int</span> x = 0; x < 10; x++<span style="color: #000000;"&gt;) {
         </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; int num = r.nextInt();</span>
         <span style="color: #0000ff;"&gt;int</span> num = r.nextInt(100) + 1<span style="color: #000000;"&gt;;
         System.out.println(num);
     }

    }
    }

System类

系统类,提供了一些有用的字段和方法

运行垃圾回收器

<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> Person {
<span style="color: #0000ff;">private<span style="color: #000000;"> String name;
<span style="color: #0000ff;">private <span style="color: #0000ff;">int<span style="color: #000000;"> age;

</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; Person() {
    </span><span style="color: #0000ff;"&gt;super</span><span style="color: #000000;"&gt;();
}

</span><span style="color: #0000ff;"&gt;public</span> Person(String name,<span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; age) {
    </span><span style="color: #0000ff;"&gt;super</span><span style="color: #000000;"&gt;();
    </span><span style="color: #0000ff;"&gt;this</span>.name =<span style="color: #000000;"&gt; name;
    </span><span style="color: #0000ff;"&gt;this</span>.age =<span style="color: #000000;"&gt; age;
}

</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; String getName() {
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; name;
}

</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; setName(String name) {
    </span><span style="color: #0000ff;"&gt;this</span>.name =<span style="color: #000000;"&gt; name;
}

</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; getAge() {
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; age;
}

</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span> setAge(<span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; age) {
    </span><span style="color: #0000ff;"&gt;this</span>.age =<span style="color: #000000;"&gt; age;
}

@Override
</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; String toString() {
    </span><span style="color: #0000ff;"&gt;return</span> "Person [name=" + name + ",age=" + age + "]"<span style="color: #000000;"&gt;;
}

<span style="color: #ff0000;"&gt;@Override
</span></span><span style="color: #ff0000;"&gt;protected void finalize() throws Throwable {
    System.out.println("当前的对象被回收了" + this);
    super</span><span style="color: #000000;"&gt;<span style="color: #ff0000;"&gt;.finalize();
}</span>

}

?

<span style="color: #008000;">/*<span style="color: #008000;">

  • System类包含一些有用的类字段和方法。它不能被实例化。

  • 方法:

  • <span style="color: #ff0000;"> public static void gc():运行垃圾回收器。

  • public static void exit(int status)

  • public static long currentTimeMillis()

  • public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> SystemDemo {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    Person p = <span style="color: #0000ff;">new Person("赵雅芝",60<span style="color: #000000;">);
    System.out.println(p);

     p </span>= <span style="color: #0000ff;"&gt;null</span>; <span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 让p不再指定堆内存</span>

    <span style="color: #000000;"> System.gc();
    }
    }

退出jvm,获取当前时间的毫秒值

<span style="color: #008000;">/*<span style="color: #008000;">

  • System类包含一些有用的类字段和方法。它不能被实例化。

  • 方法:

  • public static void gc():运行垃圾回收器。

  • public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。

  • public static long currentTimeMillis():返回以毫秒为单位的当前时间

  • public static void arraycopy(Object src,int length)
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> SystemDemo {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    <span style="color: #008000;">//<span style="color: #008000;"> System.out.println("我们喜欢林青霞(东方不败)");
    <span style="color: #008000;">//<span style="color: #008000;"> System.exit(0);
    <span style="color: #008000;">//<span style="color: #008000;"> System.out.println("我们也喜欢赵雅芝(白娘子)");

     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; System.out.println(System.currentTimeMillis());
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 单独得到这样的实际目前对我们来说意义不大
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 那么,它到底有什么作用呢?
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 要求:请大家给我统计这段程序的运行时间</span>
     <span style="color: #0000ff;"&gt;long</span> start =<span style="color: #000000;"&gt; System.currentTimeMillis();
     </span><span style="color: #0000ff;"&gt;for</span> (<span style="color: #0000ff;"&gt;int</span> x = 0; x < 100000; x++<span style="color: #000000;"&gt;) {
         System.out.println(</span>"hello" +<span style="color: #000000;"&gt; x);
     }
     </span><span style="color: #0000ff;"&gt;long</span> end =<span style="color: #000000;"&gt; System.currentTimeMillis();
     System.out.println(</span>"共耗时:" + (end - start) + "毫秒"<span style="color: #000000;"&gt;);

    }
    }

?

数组复制

<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.Arrays;

<span style="color: #008000;">/*<span style="color: #008000;">

  • System类包含一些有用的类字段和方法。它不能被实例化。

  • 方法:

  • public static void gc():运行垃圾回收器。

  • public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。

  • public static long currentTimeMillis():返回以毫秒为单位的当前时间

  • public static void arraycopy(Object src,int length)

  • 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> SystemDemo {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    <span style="color: #008000;">//<span style="color: #008000;"> 定义数组
    <span style="color: #0000ff;">int[] arr = { 11,22,<span style="color: #ff0000;">33,44,55<span style="color: #000000;"> };
    <span style="color: #0000ff;">int[] arr2 = { 6,<span style="color: #ff0000;">7,8,9,10<span style="color: #000000;"> };

     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 请大家看这个代码的意思</span>
     System.arraycopy(arr,2,arr2,1,2<span style="color: #000000;"&gt;);
    
     System.out.println(Arrays.toString(arr));
     System.out.println(Arrays.toString(arr2));<br>

    }
    }

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读