菜鸡的Java笔记 数字操作类
数字操作类
Math 类的使用
Random 类的使用
BigInteger 和 BigDecimal 类的使用
Math 是一个数学的处理程序类,在 MMath 类中提供有一系列的数学处理公式
在 Math 类中提供的所有方法都属于 static 方法,那么就表示这个类的方法可以使用静态导入完成,同时这个类中没有属于
在 Math 类中重点观察一个操作方法: public static long round(double a) 实现四舍五入的操作
范例:观察四射五入
package cn.mysterious.study3; public class MathClass { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(Math.round(100.789)); // 101 System.out.println(Math.round(15.50)); // 16 System.out.println(Math.round(-15.50)); // -15 // 如果是负数,小数位超过了0.5就会进行进位 System.out.println(Math.round(-15.51)); // -16 } }
但是现在 有一个小小的问题出现了。使用四舍五入的操作固然好,但是整个代码的操作过程里面会发现一个问题 小数位统一都没了
所以现在就会发现一点问题:此时的程序代码冰不可能被现实开发所使用
在 Math 类中提供有一个 pow() 方法: public static double pow(double a,double b)
package cn.mysterious.study3; public class MathClass { public static void main(String[] args) { System.out.println(Math.pow(10.0, 5)); // 10000.0 } }
round() 本身就是不保留小数位的四舍五入操作,所以为了可以保存准确的小数位,那么必须进行一些有效的数学处理操作
范例:实现准确的四舍五入操作
package cn.mysterious.study3; class MyMath{ /** * 实现数字的四舍五入操作 * @param num *?/ public static double round(double num,int scale) { // TODO Auto-generated method stub return Math.round(num * Math.pow(10.0, scale)) / Math.pow(10.0, scale); } } public class MathClass { public static void main(String[] args) { System.out.println(MyMath.round(10.9789, 2)); // 10.97 } }
这种代码属于工具代码
Random 类
这个类的主要功能是产生随机数使用的,在这个类中有如下的几个操作方法
NO | 方法名称 | 类型 | 简介 |
1 | public Random() | 普通 | 实例化 Random 类对象 |
2 | public Random(long seed) | 普通 | 设置种子数 |
3 | public int nextInt(int bound) | 普通 | 取得下一个不超过边界的正整数 |
4 | public double nextDouble() | 普通 | 取得下一个正小数 |
范例:观察如下的操作代码
package cn.mysterious.study3; import java.util.Random; public class RandomClass { public static void main(String[] args) { Random rand = new Random(); for (int i = 0; i < 10; i++) { System.out.print(rand.nextInt(100) + ","); } System.out.println("\n" + "RandomClass.main()"); for (int i = 0; i < 10; i++) { System.out.print(rand.nextDouble() + ","); } } }
大部分的情况下使用 int 作为随机数是最为方便的
范例:设置种子数
package cn.mysterious.study3; import java.util.Random; public class RandomClass { public static void main(String[] args) { Random rand = new Random(567); for (int i = 0; i < 10; i++) { System.out.print(rand.nextInt(100) + ","); } } }
实际上现在可以利用这个随机数实现一个 36 选 7 的操作
范例:彩票
数字不能够重复,不能够有0不能够大于36
package cn.mysterious.study3; import java.util.Random; public class RandomClass { public static void main(String[] args) throws Exception{ int data[] = new int[7]; // 生成一个数组,保存7个最终的号码 ch(data); java.util.Arrays.sort(data); print(data); } public static void print(int temp[]){ for (int i = 0; i < temp.length; i++) { System.out.print(temp[i] + ","); } } public static void ch(int temp[]){ int foot = 0; // 作为数组的操作脚标 Random rand = new Random(); while (foot < 7) { // 表示数组中的内容还没有选够 int num = rand.nextInt(37); // 选出一个数据 if (num != 0) { // 必须保证数据不能够为0 if(!isRepeat(temp, num)){ // 不存在 temp[foot ++] = num; } } } } /** * 判断内容是否存在 * @param temp[] 要判断的数组数据 * @param num 生成的数字 * @return 如果存在则返回 true 表示不可使用 */ public static boolean isRepeat(int temp [],int num){ // 判断选定的内容是否存在 for (int i = 0; i < temp.length; i++) { if (num == temp[i]) { return true; } } return false; } }
BigInteger
如果说现在有一个数字非常非常的大,已经超过了 double 的范围,那么恶最初的设计思路是将其变为String保存
而后 String 变为字符数组,一个一个字符的进行计算操作。所以在最早的时候这样大数字的操作是非常繁琐的
而 java 为了简化其操作专门提供了 java.math.BigInteger 类。 BigInteger 是 Number 的子类,表示的就是一个数字
构造方法: public BigInteger(String val)
加法操作: public BigInteger add(BigInteger val)
减法操作: public BigInteger subtract(BigInteger val)
乘法操作: public BigInteger multiply(BigInteger val)
除法操作: public BigInteger divide(BigInteger val)
除法操作: public BigInteger[] divideAndRemainder(BigInteger val)
数组第一个内容保存的是商
数组第二个内容是余数
范例:实现基本的四则运算
package cn.mysterious.study3; import java.math.BigInteger; public class BigIntegerClass { public static void main(String[] args) { // TODO Auto-generated method stub BigInteger bigA = new BigInteger("12345678998765432112345678998754321"); BigInteger bigB = new BigInteger("123456789987654321"); System.out.println("加法操作:" + bigA.add(bigB)); System.out.println("减法操作:" + bigA.subtract(bigB)); System.out.println("乘法操作:" + bigA.multiply(bigB)); System.out.println("除法操作:" + bigA.divide(bigB)); BigInteger result [] = bigA.divideAndRemainder(bigB); System.out.println("商:" + result[0] + ",余数:" + result[1]); } }
虽然java提供有 BigInteger 工具类,但是这个类不可能胜任复杂的数学计算
BigDecimal
BigDecimal 类与 BigInteger 类相同,唯一的差别在于 BigDecimal 类保存的是小数
BigDecimal 本身也属于 Number 的子类,同时在 BigDecimal 类中定义有如下的构造方法:
构造方法: public BigDecimal(BigInteger val)
构造方法: public BigDecimal(String val)
构造方法: public BigDecimal(double val)
package cn.mysterious.study3; import java.math.BigDecimal; public class BigDecimalClass { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(Double.MAX_VALUE * Double.MAX_VALUE); // Infinity BigDecimal bigA = new BigDecimal(Double.MAX_VALUE); BigDecimal bigB = new BigDecimal(Double.MAX_VALUE); System.out.println(bigA.pow(2)); System.out.println(bigA.multiply(bigB)); } }
在 BigDecimal 类里面最早也提供有四舍五入的支持,但是这个支持是利用除法实现的
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
进位模式: public static final int ROUND_HALF_UP
package cn.mysterious.study3; import java.math.BigDecimal; class MyMath{ public static double round(double num,int scale){ return new BigDecimal(num).divide(new BigDecimal(1),scale,BigDecimal.ROUND_HALF_UP).doubleValue(); } } public class BigDecimalClass { public static void main(String[] args) { System.out.println(MyMath.round(-15.5545545, 2)); System.out.println(MyMath.round(-15.51, 0)); } }
总结
1.几乎所有的编程语言都会提供有与 Math 类 类似的功能
2.Random 生成随机数操作