JavaSE-12.3.1【常用API之Math】
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 | package day3.yub4by03.p1; /* 3 常用API 3.1 Math 1、Math类概述 Math 包含执行基本数字运算的方法 2、Math中方法的调用方式 Math类中无构造方法,但内部的方法都是静态的,则可以通过 类名.方法名 进行调用 3、Math类的常用方法 public static int abs(int a) 返回参数的绝对值 public static double ceil(double a) 返回大于或等于参数的最小double值,等于一个整数,即向上取整 public static double floor(double a) 返回小于或等于参数的最大double值,等于一个整数,即向下取整 public static int round(float a) 按照四舍五入返回最接近参数的int public static int max(int a,int b) 返回两个int值中的较大值 public static int min(int a,int b) 返回两个int值中的较小值 public static double pow (double a,double b) 返回a的b次幂的值 public static double random() 返回值为double的正值,[0.0,1.0) */ public class MathDemo { public static void main(String[] args) { //绝对值 System.out.println(Math.abs( 88 )); System.out.println(Math.abs(- 88 )); //88 //向上取整(返回double) System.out.println(Math.ceil( 12.34 )); System.out.println(Math.ceil( 12.56 )); //13.0 //向下取整(返回double) System.out.println(Math.floor( 12.34 )); System.out.println(Math.floor( 12.56 )); //12.0 //四舍五入(输入float,返回int) System.out.println(Math.round( 12 .34F)); //12 System.out.println(Math.round( 12 .56F)); //13 //较大值 System.out.println(Math.max( 12 , 34 )); //34 //较小值 System.out.println(Math.min( 12 , 34 )); //12 //a的b次幂(输入double) System.out.println(Math.pow( 2.0 , 3.0 )); //8.0 //一个随机数(输出double) System.out.println(Math.random()); //[0.0, 1.0) System.out.println(Math.random() * 100 ); //[0.0, 100.0) System.out.println( ( int )(Math.random() * 100 ) ); //[0, 99] System.out.println( ( int )(Math.random() * 100 ) + 1 ); //[0, 100] } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!