您的位置:首页 > 教育 > 培训 > 数学函数,字符串

数学函数,字符串

2024/10/5 21:19:53 来源:https://blog.csdn.net/lanxingstar2011/article/details/139353120  浏览:    关键词:数学函数,字符串

目录

Math类

三角函数

指数函数 

 取整方法

 其他方法

String类

常见方法

字符串比较方法 

子串和数字与字符串的转换 


Math类

Math类在java.lang中,不用显式引入。

三角函数

    private static void triangleFunc() {double degree = Math.toDegrees(Math.PI / 3);System.out.println("Math.PI / 3 = " + degree);double radians = Math.toRadians(180);System.out.println("degree 180 = " + radians);double sin = Math.sin(radians/2);System.out.println("sin(pi) = " + sin);double cos = Math.cos(radians);System.out.println("cos(pi) = " + cos);double tan = Math.tan(radians/4);System.out.println("tan(pi) = " + tan);}

输出结果:

指数函数 

   private static void triangleFunc() {double degree = Math.toDegrees(Math.PI / 3);System.out.println("Math.PI / 3 = " + degree);double radians = Math.toRadians(180);System.out.println("degree 180 = " + radians);double sin = Math.sin(radians/2);System.out.println("sin(pi) = " + sin);double cos = Math.cos(radians);System.out.println("cos(pi) = " + cos);double tan = Math.tan(radians/4);System.out.println("tan(pi) = " + tan);}

 输出结果:

 取整方法

    private static void intFunc() {System.out.println("向上取整 0.2 = " + Math.ceil(0.2));System.out.println("向下取整0.8 = " + Math.floor(0.8));System.out.println("四舍五入 0.6 = " + Math.rint(0.6));System.out.println("四舍五入 0.5 = " + Math.round(0.5));}

 输出结果:

 其他方法

    private static void otherFunc() {System.out.println("较小的数 = " + Math.min(3,4));System.out.println("较大的数 = " + Math.max(3,4));System.out.println("相反的数 = " + Math.abs(3));System.out.println("随机数 = " + Math.random());}

 输出结果:

String类

String是一个引用类型,不是如int, char和foat的基础类型,是一个预定义的类。

常见方法

    private static void normalFunc() {String message = " Welcome to Java! ";System.out.println("The length of message = " + message.length());System.out.println("The 5th char = " + message.charAt(5));System.out.println(message.concat("Come on."));System.out.println("Upper all char:" + message.toUpperCase());System.out.println("Lower all char: " + message.toLowerCase());System.out.println(message.trim());}

输出结果:

字符串比较方法 

    private static void compareFunc() {String s = "Hello";System.out.println(s.equals("Hello"));System.out.println(s.equalsIgnoreCase("hello"));System.out.println(s.compareTo("Hell"));System.out.println(s.compareToIgnoreCase("he"));System.out.println(s.startsWith("He"));System.out.println(s.endsWith("lo"));System.out.println(s.contains("ell"));}

 输出结果:

子串和数字与字符串的转换 

    private static void otherFunc() {String what = "summer";System.out.println(what.substring(3));System.out.println(what.substring(3, 5));String float_num = "3.14";String int_num = "314";System.out.println(Integer.parseInt(int_num));System.out.println(Double.parseDouble(float_num));}

 输出结果:

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com