Java学习10.6

1.Java模块实例:JDK中的Math类

 1 public class TestMath
 2 {
 3     public static void main(String[] args) 
 4     {
 5         /*---------下面是三角运算---------*/
 6         //将弧度转换角度
 7         System.out.println("Math.toDegrees(1.57):" + Math.toDegrees(1.57)); 
 8         //将角度转换为弧度
 9         System.out.println("Math.toRadians(90):" + Math.toRadians(90));
10         //计算反余弦,返回的角度范围在 0.0 到 pi 之间。
11         System.out.println("Math.acos(0.3):" + Math.acos(1.2)); 
12             //计算反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。 
13         System.out.println("Math.asin(0.8):" + Math.asin(0.8)); 
14             //计算反正切;返回的角度范围在 -pi/2 到 pi/2 之间。 
15         System.out.println("Math.atan(2.3):" + Math.atan(2.3)); 
16         //计算三角余弦。
17         System.out.println("Math.cos(1.57):" + Math.cos(1.57)); 
18             //计算值的双曲余弦。 
19         System.out.println("Math.cosh(1.2 ):" + Math.cosh(1.2 )); 
20         //计算正弦
21         System.out.println("Math.sin(1.57 ):" + Math.sin(1.57 )); 
22         //计算双曲正弦
23         System.out.println("Math.sinh(1.2 ):" + Math.sinh(1.2 ));
24         //计算三角正切
25         System.out.println("Math.tan(0.8 ):" + Math.tan(0.8 )); 
26         //计算双曲余弦
27         System.out.println("Math.tanh(2.1 ):" + Math.tanh(2.1 )); 
28         //将矩形坐标 (x, y) 转换成极坐标 (r, thet));,返回所得角 theta。 
29         System.out.println("Math.atan2(0.1, 0.2):" + Math.atan2(0.1, 0.2));
30         /*---------下面是取整运算---------*/
31         //取整,返回小于目标数的最大整数。
32         System.out.println("Math.floor(-1.2 ):" + Math.floor(-1.2 )); 
33         //取整,返回大于目标数的最小整数。
34         System.out.println("Math.ceil(1.2):" + Math.ceil(1.2)); 
35             //四舍五入取整
36         System.out.println("Math.round(2.3 ):" + Math.round(2.3 )); 
37         /*---------下面是乘方、开方、指数运算---------*/
38         //计算平方根。
39         System.out.println("Math.sqrt(2.3 ):" + Math.sqrt(2.3 )); 
40             //计算立方根。 
41         System.out.println("Math.cbrt(9):" + Math.cbrt(9)); 
42         //返回欧拉数 e 的n次幂。
43         System.out.println("Math.exp(2):" + Math.exp(2)); 
44         //返回 sqrt(x2:" +y2),没有中间溢出或下溢。
45         System.out.println("Math.hypot(4 , 4):" + Math.hypot(4 , 4));
46         // 按照 IEEE 754 标准的规定,对两个参数进行余数运算。
47         System.out.println("Math.IEEEremainder(5 , 2):" + Math.IEEEremainder(5 , 2));
48         //计算乘方
49         System.out.println("Math.pow(3, 2):" + Math.pow(3, 2));
50         //计算自然对数
51         System.out.println("Math.log(12):" + Math.log(12)); 
52         //计算底数为 10 的对数。
53         System.out.println("Math.log10(9):" + Math.log10(9)); 
54         // 回参数与 1 之和的自然对数。 
55         System.out.println("Math.log1p(9):" + Math.log1p(9)); 
56         /*---------下面是符号相关的运算---------*/
57         //计算绝对值。
58         System.out.println("Math.abs(-4.5):" + Math.abs(-4.5));
59         //符号赋值,返回带有第二个浮点数符号的第一个浮点参数。
60         System.out.println("Math.copySign(1.2, -1.0):" + Math.copySign(1.2, -1.0));
61         //符号函数;如果参数为 0,则返回 0;如果参数大于 0,则返回 1.0;如果参数小于 0,则返回 -1.0。
62         System.out.println("Math.signum(2.3):" + Math.signum(2.3)); 
63         /*---------下面是大小相关的运算运算---------*/
64         //找出最大值
65         System.out.println("Math.max(2.3 , 4.5):" + Math.max(2.3 , 4.5));
66             //计算最小值 
67         System.out.println("Math.min(1.2 , 3.4):" + Math.min(1.2 , 3.4));
68         //返回第一个参数和第二个参数之间与第一个参数相邻的浮点数。
69         System.out.println("Math.nextAfter(1.2, 1.0):" + Math.nextAfter(1.2, 1.0));
70         //返回比目标数略大的浮点数
71         System.out.println("Math.nextUp(1.2 ):" + Math.nextUp(1.2 ));
72         //返回一个伪随机数,该值大于等于 0.0 且小于 1.0。
73         System.out.println("Math.random():" + Math.random());
74     }
75 }

总结:

关于Math类中的一些方法,主要包括有正弦、余弦、正切、余切等三角函数的求解,以及角度和弧度的转化,还包括加减乘除、开方乘方等多种数学运算。




2.自定义了一个求平方数的静方法Square
 1 public class SquareInt {
 2 
 3     public static void main(String[] args) 
 4     {
 5         int result;
 6 
 7         for (int x = 1; x <= 10; x++) 
 8         {
 9             result = square(x);
10             // Math库中也提供了求平方数的方法
11             // result=(int)Math.pow(x,2);
12             System.out.println("The square of " + x + " is " + result + "
");
13         }
14     }
15 
16     // 自定义求平方数的静态方法
17     public static int square(int y) 
18     {
19         return y * y;
20     }
21 }



3.随机数应用示例:RollDie.java
 1 // RollDie.java
 2 // Roll a six-sided die 6000 times
 3 import javax.swing.*;
 4 
 5 public class RollDie {
 6    public static void main( String args[] )
 7    {
 8       int frequency1 = 0, frequency2 = 0,
 9           frequency3 = 0, frequency4 = 0,
10           frequency5 = 0, frequency6 = 0, face;
11    
12       // summarize results
13       for ( int roll = 1; roll <= 6000; roll++ ) {
14          face = 1 + (int) ( Math.random() * 6 );
15    
16          switch ( face ) {
17             case 1:
18                ++frequency1;
19                break;
20             case 2:
21                ++frequency2;
22                break;
23             case 3:
24                ++frequency3;
25                break;
26             case 4:
27                ++frequency4;
28                break;
29             case 5:
30                ++frequency5;
31                break;
32             case 6:
33                ++frequency6;
34                break;
35          }
36       }
37 
38       JTextArea outputArea = new JTextArea( 7, 10 );
39 
40       outputArea.setText(
41          "Face	Frequency" +
42          "
1	" + frequency1 +
43          "
2	" + frequency2 +
44          "
3	" + frequency3 +
45          "
4	" + frequency4 +
46          "
5	" + frequency5 +
47          "
6	" + frequency6 );
48 
49       JOptionPane.showMessageDialog( null, outputArea,
50          "Rolling a Die 6000 Times",
51          JOptionPane.INFORMATION_MESSAGE );
52       System.exit( 0 );
53    }
54 }

原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/13799065.html