Math
来源:互联网 发布:论坛源码3.4 编辑:程序博客网 时间:2024/06/15 17:00
1 /* 2 * Math:用于数学运算的类。 3 * 成员变量: 4 * public static final double PI 5 * public static final double E 6 * 成员方法: 7 * public static int abs(int a):绝对值 8 * public static double ceil(double a):向上取整 9 * public static double floor(double a):向下取整10 * public static int max(int a,int b):最大值 (min自学)11 * public static double pow(double a,double b):a的b次幂12 * public static double random():随机数 [0.0,1.0)13 * public static int round(float a) 四舍五入(参数为double的自学)14 * public static double sqrt(double a):正平方根15 */16 public class MathDemo {17 public static void main(String[] args) {18 // public static final double PI19 System.out.println("PI:" + Math.PI);20 // public static final double E21 System.out.println("E:" + Math.E);22 System.out.println("--------------");23 24 // public static int abs(int a):绝对值25 System.out.println("abs:" + Math.abs(10));26 System.out.println("abs:" + Math.abs(-10));27 System.out.println("--------------");28 29 // public static double ceil(double a):向上取整30 System.out.println("ceil:" + Math.ceil(12.34));31 System.out.println("ceil:" + Math.ceil(12.56));32 System.out.println("--------------");33 34 // public static double floor(double a):向下取整35 System.out.println("floor:" + Math.floor(12.34));36 System.out.println("floor:" + Math.floor(12.56));37 System.out.println("--------------");38 39 // public static int max(int a,int b):最大值40 System.out.println("max:" + Math.max(12, 23));41 // 需求:我要获取三个数据中的最大值42 // 方法的嵌套调用43 System.out.println("max:" + Math.max(Math.max(12, 23), 18));44 // 需求:我要获取四个数据中的最大值45 System.out.println("max:"46 + Math.max(Math.max(12, 78), Math.max(34, 56)));47 System.out.println("--------------");48 49 // public static double pow(double a,double b):a的b次幂50 System.out.println("pow:" + Math.pow(2, 3));51 System.out.println("--------------");52 53 // public static double random():随机数 [0.0,1.0)54 System.out.println("random:" + Math.random());55 // 获取一个1-100之间的随机数56 System.out.println("random:" + ((int) (Math.random() * 100) + 1));57 System.out.println("--------------");58 59 // public static int round(float a) 四舍五入(参数为double的自学)60 System.out.println("round:" + Math.round(12.34f));61 System.out.println("round:" + Math.round(12.56f));62 System.out.println("--------------");63 64 //public static double sqrt(double a):正平方根65 System.out.println("sqrt:"+Math.sqrt(4));66 }67 }
练习:
1 /* 2 * 需求:请设计一个方法,可以实现获取任意范围内的随机数。 3 * 4 * 分析: 5 * A:键盘录入两个数据。 6 * int strat; 7 * int end; 8 * B:想办法获取在start到end之间的随机数 9 * 我写一个功能实现这个效果,得到一个随机数。(int)10 * C:输出这个随机数11 */12 public class MathDemo {13 public static void main(String[] args) {14 Scanner sc = new Scanner(System.in);15 System.out.println("请输入开始数:");16 int start = sc.nextInt();17 System.out.println("请输入结束数:");18 int end = sc.nextInt();19 20 for (int x = 0; x < 100; x++) {21 // 调用功能22 int num = getRandom(start, end);23 // 输出结果24 System.out.println(num);25 }26 }27 28 /*29 * 写一个功能 两个明确: 返回值类型:int 参数列表:int start,int end30 */31 public static int getRandom(int start, int end) {32 // 回想我们讲过的1-100之间的随机数33 // int number = (int) (Math.random() * 100) + 1;34 // int number = (int) (Math.random() * end) + start;35 // 发现有问题了,怎么办呢?36 int number = (int) (Math.random() * (end - start + 1)) + start;37 return number;38 }39 }
0 0
- Math
- math
- Math
- Math
- Math
- Math
- math
- Math
- math
- Math
- Math
- Math
- Math
- Math
- Math
- Math
- Math
- Math
- 二分查找法
- 正则表达式
- Linux IO函数
- Java总结篇系列:Java多线程(二)
- Random:产生随机数的类
- Math
- Calendar
- Random
- 成为Java顶尖程序员 ,看这11本书就够了
- 集合Collection
- List
- 申请密钥 Android SDK
- SpringMvc JSON 406,吐血。。。。
- spring 官方下载地址(Spring Framework 3.2.x&Spring Framework 4.0.x)