JAVASE基础模块二十(Math System 常用类)

JAVASE基础模块二十(Math System 常用类)

Math

  • 常用数学方法

    public class Mat {
        public static void main(String[] args) {
            System.out.println(Math.PI);//圆周率
            System.out.println(Math.E);//自然对数e
            System.out.println(Math.abs(-1));//相反数
            System.out.println(Math.ceil(3.1));//向上取整
            System.out.println(Math.floor(3.4));//向下取整
            System.out.println(Math.random());//随机数
            System.out.println(Math.max(66, 77));//最大值
            System.out.println(Math.min(3, 7));//最小值
            System.out.println(Math.pow(3, 3));//3的3次方
            System.out.println(Math.sqrt(9));//开根号9
            System.out.println(Math.round(3.6));//四舍五入
            System.out.println(Math.pow(8, 1/3.0));//开三次根号8 即8的1/3次方
    
    
        }
    }
    运行结果:
    3.141592653589793
    2.718281828459045
    1
    4.0
    3.0
    0.631757479119988
    77
    3
    27.0
    3.0
    4
    2.0
    
    进程已结束,退出代码0
    
  • Random 可以生产一些随机数据

    1. Random() 创建一个新的随机数生成器
    import java.util.Random;
    public class SuiJi {
        public static void main(String[] args) {
            Random s = new Random();
            for (int i = 0; i < 10; i++) {
            //生成10以内的随机数 不包括10
                int n = s.nextInt();
                int n1 = s.nextInt(10);
                System.out.print(n1 + "\t");
            }
            System.out.println();
            //生成随机小数
            for (int i = 0; i < 10; i++) {
                double c = s.nextDouble(10);
                System.out.print(c + "\t");
            }
            System.out.println();
            //生成随机boolean结果
            for (int i = 0; i < 10; i++) {
                boolean d = s.nextBoolean();
                System.out.print(d + "\t");
            }
    
        }
    }
    运行结果:
    0	6	0	4	5	1	8	0	4	3	
    0.9612622209326054	0.028419643402946226	0.970400836746776	0.650129681131199	0.0464290713465646	0.7594233858710998	0.05472876539698879	0.9022535461223241	0.37485397139752463	0.7533694820433411	
    false	true	true	false	false	false	true	false	false	true	
    Process finished with exit code 0
    
    1. Random( long seed) 使用单个 long 种子创建一个新的随机数生成器

      import java.util.Random;
      public class Kcan {
          public static void main(String[] args) {
              //不给种子 每次结果不一样
              Random s = new Random();
              for (int i = 0; i < 10; i++) {
                  int ii = s.nextInt(10);
                  System.out.print(ii + "\t");
              }
              System.out.println();
              //给了种子就会算出一些随机数 每次运行的结果一样
              Random s1 = new Random(3l);
              for (int i1 = 0; i1 < 10; i1++) {
                  int ii1 = s1.nextInt(10);
                  System.out.print(ii1 + "\t");
              }
          }
      }
      第一次运行结果:
      9	1	4	2	8	5	0	6	5	0	
      4	0	0	1	8	2	9	4	9	1	
      Process finished with exit code 0
      第二次运行结果:
      6	1	7	6	9	4	2	6	4	3	
      4	0	0	1	8	2	9	4	9	1	
      Process finished with exit code 0
      第三次运行结果:
      1	1	1	8	1	3	9	0	8	7	
      4	0	0	1	8	2	9	4	9	1	
      Process finished with exit code 0
      
  • void nextBytes ( byte[] bytes) 生成随机字节并将其置于用户提供的 byte 数组中

import java.util.Arrays;
import java.util.Random;
public class DD {
    public static void main(String[] args) {
        Random random = new Random();
        byte[] bytes = new byte[10];
        random.nextBytes(bytes);
        System.out.println(Arrays.toString(bytes));
    }
}
运行结果:
[119, 98, 114, -30, 74, 49, -71, 83, 100, 42]

Process finished with exit code 0

System

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

  • in public static final InputStream in“标准”输入流。此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源

  • out public static final PrintStream out“标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标

  • 对于简单独立的 Java 应用程序,编写一行输出数据的典型方式是:System.out.println(data)

    public class MyTest {
        public static void main(String[] args) {
            PrintStream out = System.out;
            out.println("abc");
            System.out.println(200);
            System.out.println("=============================");
            System.err.println("错误信息");
        }
    }
    运行结果:
    abc
    200
    =============================
    错误信息(红色)
    
    Process finished with exit code 0
    
    
  • exit(0); 退出虚拟机 0 正常退出 非0 强制退出

    public class Syst {
        public static void main(String[] args) {
            System.out.println("123456");
            System.out.println("123456");
            System.out.println("123456");
            System.exit(0);
            System.out.println("123456");
            System.out.println("123456");
            System.out.println("123456");
        }
    }
    运行结果:
    123456
    123456
    123456
    
    进程已结束,退出代码0
    
  • System.currentTimeMillis(); 获取当前系统时间的毫秒值(获取从 1970 01-01 00:00:00 到现在的间隔的 毫秒值)

    public class HaoMiao {
        public static void main(String[] args) {
            long timeMillis=System.currentTimeMillis();
            System.out.println(timeMillis);
            //测试循环的耗时
            for (int i = 0; i <100 ; i++) {
                System.out.print(i+"\t");
            }
            long timeMillis1=System.currentTimeMillis();
            System.out.println(timeMillis1-timeMillis);
        }
    }
    运行结果:
    1596249190893
    0	1	2	3	4	5	6	7	8	9	10	11	12	13	14	15	16	17	18	19	20	21	22	23	24	25	26	27	28	29	30	31	32	33	34	35	36	37	38	39	40	41	42	43	44	45	46	47	48	49	50	51	52	53	54	55	56	57	58	59	60	61	62	63	64	65	66	67	68	69	70	71	72	73	74	75	76	77	78	79	80	81	82	83	84	85	86	87	88	89	90	91	92	93	94	95	96	97	98	99	
    时间:15
    
    Process finished with exit code 0
    
  • 垃圾回收器 System.gc(); Runtime.getRuntime().gc(); 回收垃圾

    public class LaJi {
        public static void main(String[] args) {
            //运行垃圾回收器 回收垃圾
            System.gc();
            Runtime.getRuntime().gc();
            //ctrl+alt+空格
            System.out.println(System.getProperty("java.version"));
            System.out.println(System.getProperty("java.class.path"));
    
        }
    }
    运行结果:
    13.0.2
    D:\IDEA\IdeaProjects\System\out\production\System
    
    Process finished with exit code 0
    

待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值