Java中的常用类详解(Math、Scanner、Random、String)

目录

一、Math(数学类)

自带常量

取整方法

三角函数方法

指数函数方法

其他方法

二、Scanner(实用程序类)

三、Random(随机数类)

四、String(字符串类)

获取相关

判断相关

修改相关


一、Math(数学类)

math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

自带常量

  • static double E :比任何其他值都更接近 e(即自然对数的底数)的 double 值。

  • static double PI : 比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。

取整方法

  • static double ceil(double a) :返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。

  • static double floor(double a) :返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。

  • static double rint(double a) :返回最接近参数并等于某一整数的 double 值。

  • static long round(double a) :返回最接近参数的 long

三角函数方法

  • static double sin(double a) :返回角的三角正弦。

  • static double cos(double a) :返回角的三角余弦。

  • static double tan(double a) :返回角的三角正切。

  • static double toDegrees(double angrad) :将用弧度表示的角转换为近似相等的用角度表示的角。

  • static double toRadians(double angdeg) :将用角度表示的角转换为近似相等的用弧度表示的角。

  • static double asin(double a) :返回一个值的反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。

  • static double acos(double a) :返回一个值的反余弦;返回的角度范围在 0.0 到 pi 之间。

  • static double atan(double a) :返回一个值的反正切;返回的角度范围在 -pi/2 到 pi/2 之间。

指数函数方法

  • static double exp(double a) :返回欧拉数 edouble 次幂的值。

  • static double log(double a) :返回 double 值的自然对数(底数是 e)。

  • static double log10(double a) :返回 double 值的底数为 10 的对数。

  • static double pow(double a, double b) :返回第一个参数的第二个参数次幂的值。

  • static double sqrt(double a) :返回正确舍入的 double 值的正平方根。

  • static double cbrt(double a) :返回 double 值的立方根。

其他方法

  • static double abs(double a) :返回 double 值的绝对值。

  • static double hypot(double x, double y) :返回 sqrt(x2 +y2),没有中间溢出或下溢。

  • static double max(double a, double b) :返回两个 double 值中较大的一个。

  • static double min(double a, double b) :返回两个 double 值中较小的一个。

  • static double random() :返回带正号的 double 值,该值大于等于 0.0 且小于 1.0

exam1:获取指定数据的最大值和最小值

public class MathDemo01 {
    public static void main(String[] args) {
        int a = 5;
        int b = 10;
        int max = Math.max(a, b);
        System.out.println("max = " + max);

        int min = Math.min(a, b);
        System.out.println("min = " + min);
    }
}

exam2:产生随机数

public class MathDemo02 {
    public static void main(String[] args) {
        double random = Math.random();
        System.out.println(random);
    }
}

得到的是0·1之间的小数,不会超过1。

exam3:对小数进行操作


/**
 * static double ceil(double a)  :返回最小的(最接近负无穷大)`double` 值,该值大于等于参数,并等于某个整数。
 * static double floor(double a)  :返回最大的(最接近正无穷大)`double` 值,该值小于等于参数,并等于某个整数。
 * static long round(double a)  :返回最接近参数的 `long`。
 */
public class MathDemo03 {
    public static void main(String[] args) {
        double d1 = 3.189;
        double d2 = 3.897;
        double d3 = -3.189;
        double d4 = -3.897;
        // ceil 方法是得到比给定值大的最小的整数
        double c1 = Math.ceil(d1); // 4.0
        double c2 = Math.ceil(d2); // 4.0
        double c3 = Math.ceil(d3); // -3
        double c4 = Math.ceil(d4); // -3

        System.out.println(c1 + "\t" + c2 + "\t" + c3 + "\t" + c4);

        // floor 得到给值小的最大的整数
        double f1 = Math.floor(d1); // 3
        double f2 = Math.floor(d2); // 3
        double f3 = Math.floor(d3); // -4
        double f4 = Math.floor(d4); // -4
        System.out.println(f1 + "\t" + f2 + "\t" + f3 + "\t" + f4);


        long r1 = Math.round(d1); // 3
        long r2 = Math.round(d2); // 4
        long r3 = Math.round(d3); // -3
        long r4 = Math.round(d4); // -4
        System.out.println(r1 + "\t" + r2 + "\t" + r3 + "\t" + r4);
    }
}

二、Scanner(实用程序类)

一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器。

Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配。然后可以使用不同的 next 方法将得到的标记转换为不同类型的值。

package com;

import java.util.Scanner;

/**
 * @description: TODO
 * @company: 倩女幽魂
 * @author: Jock
 * @date: 2024-03-24
 * @version: 1.0
 */
public class ScannerDemo01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 获取输入的一行数据
        //String line = sc.nextLine();
        //System.out.println("line = " + line);

        //int num = sc.nextInt();
        //System.out.println("num = " + num);

        while (sc.hasNext()) {
            String next = sc.next();
            System.out.println("next = " + next);
            if (next.equals("abc")) break;
        }

    }
}

三、Random(随机数类)

这个类是用于产生一个随机数的。

public class RandomDemo01 {
    public static void main(String[] args) {
        Random random = new Random();

        int i = random.nextInt(100);
        System.out.println("i = " + i);

        double v = random.nextDouble();
        System.out.println("v = " + v);
    }
}

四、String(字符串类)

String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。 字符串是常量;它们的值在创建之后不能更改,因为 String 对象是不可变的,所以可以共享。

获取相关

  • char charAt(int index) :返回指定索引处的 char 值。

  • int indexOf(int ch) :返回指定字符在此字符串中第一次出现处的索引。

  • int indexOf(int ch, int fromIndex) :返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

  • int indexOf(String str) :返回指定子字符串在此字符串中第一次出现处的索引。

  • int indexOf(String str, int fromIndex) :返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。

  • int lastIndexOf(int ch) :返回指定字符在此字符串中最后一次出现处的索引。

  • int length() :返回此字符串的长度。

  • String[] split(String regex) : 根据给定正则表达式的匹配拆分此字符串。

  • String substring(int beginIndex) :返回一个新的字符串,它是此字符串的一个子字符串。

  • String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串的一个子字符串。


/**
 * - char charAt(int index)  :返回指定索引处的 `char` 值。
 * - int indexOf(int ch)  :返回指定字符在此字符串中第一次出现处的索引。
 * - int indexOf(int ch, int fromIndex)  :返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
 * - int indexOf(String str)  :返回指定子字符串在此字符串中第一次出现处的索引。
 * - int indexOf(String str, int fromIndex)  :返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
 * - int lastIndexOf(int ch)  :返回指定字符在此字符串中最后一次出现处的索引。
 * - int length()  :返回此字符串的长度。
 * - String[] split(String regex)  : 根据给定正则表达式的匹配拆分此字符串。
 * - String substring(int beginIndex)  :返回一个新的字符串,它是此字符串的一个子字符串。
 * - String substring(int beginIndex, int endIndex)  :返回一个新字符串,它是此字符串的一个子字符串。
 */
public class StringDemo01 {
    public static void main(String[] args) {
        String str = "hello world";

        // char charAt(int index),它是从 0 开始,也就是第一个字符的下标是0
        char c = str.charAt(4);
        System.out.println("c = " + c);

        // int indexOf(int ch):如果能找到就返回对应的下标,否则返回 -1
        int index = str.indexOf("W");
        System.out.println("index = " + index);

        //int indexOf(String str, int fromIndex)
        int index1 = str.indexOf("o", 6);
        System.out.println("index1 = " + index1);

        // int lastIndexOf(int ch) 从后往前找
        int index2 = str.lastIndexOf('o');
        System.out.println("index2 = " + index2);

        // int length():获取字符串长度
        int length = str.length();
        System.out.println("length = " + length);

        // String[] split(String regex)
        String[] s = str.split(" ");
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }

        // String substring(int beginIndex, int endIndex)截取字符串
        String substring = str.substring(3, 9);
        System.out.println("substring = " + substring);
    }
}

判断相关

  • int compareTo(String anotherString) :按字典顺序比较两个字符串。

  • int compareToIgnoreCase(String str) :按字典顺序比较两个字符串,不考虑大小写。

  • boolean contains(CharSequence s) :当且仅当此字符串包含指定的 char 值序列时,返回 true。

  • boolean endsWith(String suffix) :测试此字符串是否以指定的后缀结束。

  • boolean equals(Object anObject) :将此字符串与指定的对象比较。

  • boolean equalsIgnoreCase(String anotherString) :将此 String 与另一个 String 比较,不考虑大小写。

  • boolean isEmpty() :当且仅当 length()0 时返回 true

  • boolean startsWith(String prefix) :测试此字符串是否以指定的前缀开始。

  • boolean startsWith(String prefix, int toffset) :测试此字符串从指定索引开始的子字符串是否以指定前缀开始。


public class StringDemo02 {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "Hello";
        String s3 = "hello";
        String s4 = new String("hello");

        System.out.println((int)'h');
        System.out.println((int)'H');

        //- int compareTo(String anotherString)  :按字典顺序比较两个字符串。它会返回正数、零、负数
        int i = s1.compareTo(s2);
        System.out.println("i = " + i);

        //- int compareToIgnoreCase(String str)  :按字典顺序比较两个字符串,不考虑大小写。
        int i1 = s1.compareToIgnoreCase(s2);
        System.out.println("i1 = " + i1);

        //- boolean contains(CharSequence s)  :当且仅当此字符串包含指定的 char 值序列时,返回 true。
        boolean llo = s1.contains("lloo");
        System.out.println("llo = " + llo);

        //- boolean endsWith(String suffix)  :测试此字符串是否以指定的后缀结束。
        boolean lo = s1.endsWith("Lo");
        System.out.println("lo = " + lo);

        //- boolean equals(Object anObject)  :将此字符串与指定的对象比较。
        boolean equals3 = s1.equals(s2);
        boolean equals = equals3;
        System.out.println("equals = " + equals);
        boolean equals1 = s1.equals(s3);
        System.out.println("equals1 = " + equals1);
        boolean equals2 = s3.equals(s4);
        System.out.println("equals2 = " + equals2);

        boolean flag = s1 == s3;
        System.out.println("flag = " + flag);

        flag = s1 == s4;
        System.out.println("flag = " + flag);

        // == 在 java 中比较对象的引用地址,而 equals 是比较对象的值


        //- boolean equalsIgnoreCase(String anotherString)  :将此 `String` 与另一个 `String` 比较,不考虑大小写。
        flag = s1.equalsIgnoreCase(s3);
        System.out.println("flag = " + flag);

        //- boolean isEmpty()  :当且仅当 [`length()`](../../java/lang/String.html#length()) 为  `0` 时返回 `true`。
        System.out.println("s1.isEmpty() = " + s1.isEmpty());

        //- boolean startsWith(String prefix)  :测试此字符串是否以指定的前缀开始。
        boolean h = s1.startsWith("H");
        System.out.println("h = " + h);

        //- boolean startsWith(String prefix, int toffset)  :测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
    }
}

修改相关

  • String replace(char oldChar, char newChar) :返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

  • String replace(CharSequence target, CharSequence replacement) :使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。

  • String toLowerCase() :使用默认语言环境的规则将此 String 中的所有字符都转换为小写。

  • String toUpperCase() :使用默认语言环境的规则将此 String 中的所有字符都转换为大写。

  • String trim() :返回字符串的副本,忽略前导空白和尾部空白。


/**
 * - String replace(char oldChar, char newChar)  :返回一个新的字符串,它是通过用 `newChar` 替换此字符串中出现的所有 `oldChar` 得到的。
 * - String replace(CharSequence target, CharSequence replacement)  :使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
 * - String toLowerCase() :使用默认语言环境的规则将此 `String` 中的所有字符都转换为小写。
 * - String toUpperCase()  :使用默认语言环境的规则将此 `String` 中的所有字符都转换为大写。
 * - String trim() :返回字符串的副本,忽略前导空白和尾部空白。
 */
public class StringDemo03 {
    public static void main(String[] args) {
        String str = " good AFTERNOON  ";

        //- String replace(char oldChar, char newChar)  :返回一个新的字符串,它是通过用 `newChar` 替换此字符串中出现的所有 `oldChar` 得到的。
        //- String replace(CharSequence target, CharSequence replacement)  :使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
        String replace = str.replace("good", "Good");
        System.out.println("str = " + str);
        System.out.println("replace = " + replace);

        //- String toLowerCase() :使用默认语言环境的规则将此 `String` 中的所有字符都转换为小写。
        String lowerCase = str.toLowerCase();
        System.out.println("lowerCase = " + lowerCase);

        //- String toUpperCase()  :使用默认语言环境的规则将此 `String` 中的所有字符都转换为大写。
        String upperCase = str.toUpperCase();
        System.out.println("upperCase = " + upperCase);

        //- String trim() :返回字符串的副本,忽略前导空白和尾部空白。
        String trim = str.trim();
        System.out.println("trim = " + trim);
    }
}
  • 31
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Smiling Mr. Rui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值