/* * Math:用于数学运算的类。 * 成员变量: * public static final double PI * public static final double E * 成员方法: * public static int abs(int a):绝对值 * public static double ceil(double a):向上取整 * public static double floor(double a):向下取整 * public static int max(int a,int b):最大值 (min自学) * public static double pow(double a,double b):a的b次幂 * public static double random():随机数 [0.0,1.0) * public static int round(float a) 四舍五入(参数为double的自学) * public static double sqrt(double a):正平方根 */ public class MathDemo { public static void main(String[] args) { // public static final double PI System.out.println("PI:" + Math.PI); // public static final double E System.out.println("E:" + Math.E); System.out.println("--------------"); // public static int abs(int a):绝对值 System.out.println("abs:" + Math.abs(10)); System.out.println("abs:" + Math.abs(-10)); System.out.println("--------------"); // public static double ceil(double a):向上取整 System.out.println("ceil:" + Math.ceil(12.34)); System.out.println("ceil:" + Math.ceil(12.56)); System.out.println("--------------"); // public static double floor(double a):向下取整 System.out.println("floor:" + Math.floor(12.34)); System.out.println("floor:" + Math.floor(12.56)); System.out.println("--------------"); // public static int max(int a,int b):最大值 System.out.println("max:" + Math.max(12, 23)); // 需求:我要获取三个数据中的最大值 // 方法的嵌套调用 System.out.println("max:" + Math.max(Math.max(12, 23), 18)); // 需求:我要获取四个数据中的最大值 System.out.println("max:" + Math.max(Math.max(12, 78), Math.max(34, 56))); System.out.println("--------------"); // public static double pow(double a,double b):a的b次幂 System.out.println("pow:" + Math.pow(2, 3)); System.out.println("--------------"); // public static double random():随机数 [0.0,1.0) System.out.println("random:" + Math.random()); // 获取一个1-100之间的随机数 System.out.println("random:" + ((int) (Math.random() * 100) + 1)); System.out.println("--------------"); // public static int round(float a) 四舍五入(参数为double的自学) System.out.println("round:" + Math.round(12.34f)); System.out.println("round:" + Math.round(12.56f)); System.out.println("--------------"); //public static double sqrt(double a):正平方根 System.out.println("sqrt:"+Math.sqrt(4)); } } -----------------------Random 随机数 * Random:产生随机数的类 * * 构造方法: * public Random():没有给种子,用的是默认种子,是当前时间的毫秒值 * public Random(long seed):给出指定的种子 * * 给定种子后,每次得到的随机数是相同的。 * * 成员方法: * public int nextInt():返回的是int范围内的随机数 * public int nextInt(int n):返回的是[0,n)范围的内随机数 */ public class RandomDemo { public static void main(String[] args) { // 创建对象 // Random r = new Random(); Random r = new Random(1111); for (int x = 0; x < 10; x++) { // int num = r.nextInt(); int num = r.nextInt(100) + 1; System.out.println(num); } } }
------------把字节数组转成字符串 // public String(byte[] bytes) byte[] bys = { 97, 98, 99, 100, 101 }; String s2 = new String(bys); ----------把字节数组的一部分转成字符串 // public String(byte[] bytes,int index,int length): // 我想得到字符串"bcd" String s3 = new String(bys, 1, 3); --------------把字符数组转成字符串 // public String(char[] value): char[] chs = { 'a', 'b', 'c', 'd', 'e', '爱', '林', '亲' }; String s4 = new String(chs); --------------- String类的获取功能 * int length():获取字符串的长度。 * char charAt(int index):获取指定索引位置的字符 * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。 * 为什么这里是int类型,而不是char类型? * 原因是:'a'和97其实都可以代表'a' * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。 * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。 * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。 * String substring(int start):从指定位置开始截取字符串,默认到末尾。 * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。 */ -------------------- String的转换功能: * byte[] getBytes():把字符串转换为字节数组。 //byte[] bys = s.getBytes(); * char[] toCharArray():把字符串转换为字符数组。 //char[] chs = s.toCharArray(); * static String valueOf(char[] chs):把字符数组转成字符串。//String ss = String.valueOf(chs) * static String valueOf(int i):把int类型的数据转成字符串。//String sss = String.valueOf(i); * 注意:String类的valueOf方法可以把任意类型的数据转成字符串。 * String toLowerCase():把字符串转成小写。 * String toUpperCase():把字符串转成大写。 * String concat(String str):把字符串拼接。s1.concat(s2); -------------------- * 替换功能: * String replace(char old,char new) * String replace(String old,String new) * * 去除字符串两空格 * String trim() * * 按字典顺序比较两个字符串 * int compareTo(String str) * int compareToIgnoreCase(String str) ----------------* StringBuffer: 使用: StringBuffer sb = new StringBuffer(); ----- public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身 * * public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身(int输入的是插入的第一个字符是第几个位置) */ -----------StringBuffer的删除功能 * public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身 * public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身 ------StringBuffer的替换功能: * public StringBuffer replace(int start,int end,String str):从start开始到end用str替换 -------------* StringBuffer的反转功能:(顺序反转) * public StringBuffer reverse() --------------* StringBuffer的截取功能:注意返回值类型不再是StringBuffer本身了 * public String substring(int start) * public String substring(int start,int end)
java基础复习-Math和String StringBuffer类使用
最新推荐文章于 2024-05-13 13:21:36 发布