zoukankan      html  css  js  c++  java
  • Math

     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 PI
    19         System.out.println("PI:" + Math.PI);
    20         // public static final double E
    21         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 end
    30      */
    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 }
  • 相关阅读:
    在泛型方法中返回泛型数组
    java泛型方法返回泛型结果
    hive -e和hive -f的区别(转)
    hive表批处理
    python网络编程——实现简单聊天
    python网络编程(转)
    python分布式编程(转)
    shell多线程之进程间通信(3)
    在xml文件中使用该控件
    Java文件中代码
  • 原文地址:https://www.cnblogs.com/jiangjianzhu/p/5807181.html
Copyright © 2011-2022 走看看