第四章 常用类
文章目录
4.1 Math类
Math类是用于数学计算的一个工具类
对于工具类而言,里面的大部分成员都是静态的static
自带常量
1.static double E:自然对数
2.static double PI:圆周率
System.out.println(Math.E);
System.out.println(Math.PI);
取整方法
1.static double ceil(double a):向上取整
2.static double floor(double a):向下取整
3.static long round(double a):四舍五入
System.out.println(Math.ceil(3.1));
System.out.println(Math.ceil(3.9));
System.out.println(Math.ceil(-3.1));
System.out.println(Math.ceil(-3.9));
System.out.println(Math.floor(3.1));
System.out.println(Math.floor(3.9));
System.out.println(Math.floor(-3.1));
System.out.println(Math.floor(-3.9));
System.out.println(Math.round(3.1));
System.out.println(Math.round(3.9));
System.out.println(Math.round(-3.1));
System.out.println(Math.round(-3.9));
三角函数
1.static double sin(double a):正弦函数 参数是弧度值
2.static double cos(double a): 余弦函数
3.static double tan(double a):正切函数
4.static double toDegrees(double a):将弧度转为角度
5.static double toRadians(double a):将角度转为弧度
6.static double asin(double a):反正弦函数
7.static double acos(double angles):反余弦函数
8.static double atan(double a):反正切函数
System.out.println(Math.sin(Math.PI/6));
System.out.println(Math.cos(Math.PI/3));
System.out.println(Math.tan(Math.PI/4));
System.out.println(Math.toDegrees(Math.PI/2));
System.out.println(Math.toRadians(90));
指数函数
1.static double pow (double a , double b):求a的b次幂
2.static double sqrt(double a):求a的平方根
3.static double cbrt(double a):求立方根
System.out.println(Math.pow(2,3));
System.out.println(Math.sqrt(4));
System.out.println(Math.cbrt(8));
其他方法
1.static double abs(double a):求a的绝对值
2.static double hypot(double deltX , double deltY) :放回两点之间的距离
3.static double max(a,b):返回a和b之间的最大值
4.static double min(a,b):返回a和b之间的最小值
5.static double random():返回(0,1)之间的小数
System.out.println(Math.abs(-1));
System.out.println(Math.hypot(0 - 1,0 - - 1));
public class Demo53{
public static void main(String[] args){
double a = 3;
double b = 4;
double c = 5;
double a1 = Math.pow(a,2);
double b1 = Math.pow(b,2);
double c1 = Math.pow(c,2);
double A = Math.round(Math.toDegrees(Math.acos((a1 - b1 - c1) / (-2 * b * c))));
double B = Math.round(Math.toDegrees(Math.acos((b1 - a1 - c1) / (-2 * a * c))));
double C = Math.round(Math.toDegrees(Math.acos((c1 - b1 - a1) / (-2 * a * b))));
System.out.println(A);
System.out.println(B);
System.out.println(C);
}
}
4.2 Scanner类
主要用于负责数据输入的类,底层是和IO流相关
- String next():获取直到遇到空格为止的一个字符串
import java.util.Scanner;
public class Demo53{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("输入一句话:");
String line = input.nextLine();
System.out.println(line);
System.out.print("输入三个单词:");
String word1 = input.next();
String word2 = input.next();
String word3 = input.next();
System.out.println(word1);
System.out.println(word2);
System.out.println(word3);
}
}
- String nextLine():获取直到遇到回车为止的一个字符串
- .int nextInt():获取下一个整数 byte short long
- double nextDouble()
- boolean nextBoolean()
- float nextFloat()
4.3 Random类
主要用于产生随机数
- boolean nextBoolean():随机产生一个布尔类型值
- double nextDouble():随机生成0.0 ~1.0之间的小数
- double nextInt():随机生成一个0 ~2^32的整数
- double nextInt(n):随机生成一个[0,n)的整数
import java.util.Random;
public class Sample{
public static void main(String[] args){
Random random = new Random();
for (int i = 1;i <= 10;i++){
System.out.print(random.nextDouble() * 4.0 + " " );
}
for (int i = 1;i <= 10;i++){
System.out.print(random.nextDouble() + " " );
}
for (int i = 1;i <= 10;i++){
System.out.print(random.nextInt() + " " );
}
for (int i = 1;i <= 10;i++){
System.out.print(random.nextInt(2) + " " );
}
for (int i = 1;i <= 10;i++){
System.out.print(random.nextBoolean() + " " );
}
}
}
4.4 String类
String是一个类,它描述的是字符串。在Java代码当中,所有字符常量(字符字面量)都是String类的一个实例对象。并且字符串一旦创建,则不可修改!不可修改长度,不可修改其内容。所以将来对字符串内容的改变,不能改变原来的,只能创建一个新的字符串。
public class Sample {
public static void main(String[] args){
String s1 = "abc";
String s2 = "ab" + "dc";
//上述代码一共有几个字符串?
//四个“abc”“ ab”"dc"" abdc"
}
}
获取相关
- char charAt(int index):获取角标处的字符
- int indexOf(int ch):获取指定字符(编码)在字符串中第一次(从左到右)出现的地方,返回的是角标。如果是负一,不存在。
- int lastIndexOf(int ch):获取指定字符(编码)在字符串中第一次(从右到左)出现的地方,返回的是角标
- int indexOf(String str):获取指定字符串在本字符串中第一次(从左到右)出现的地方,返回的是角标
- int lastindexOf(String str):获取指定字符串在本字符串中第一次(从右到左)出现的地方,返回的是角标
- int length():获取字符串的长度(字符的个数)
- String[] split(String regex):将字符串按照regex的定义进行切割.regex指正则表达式
- String substring(int beginIndex):截取一段子字符串,从beginIndex开始到结尾
- String substring(int beginString,int endIndex):截取一段子字符串,从beginIndex开始到endIndex(不包含)
public class Sample{
public static void main(String[] args){
String str = "banana orange apple watermelon";
System.out.println(str.charAt(1));
System.out.println(str.indexOf('o'));
System.out.println(str.lastIndexOf(97));
System.out.println(str.length());
System.out.println(str.substring(6));
System.out.println(str.substring(6,9));
String[] arr = str.split(" ");
System.out.println(arr[2]);
System.out.println(str.indexOf("anan"));
}
}
判断相关
- int compareTo(String anotherString):按照字典顺序比较两个字符串的大小
- boolean contains(String another):判断当前字符串中是否包含指定字符串another
- boolean equals(String another):比较当前字符串与指定字符串的内容是否相同
- boolean isEmpty(): 判断当前字符串是否为空 length() ==0
- boolean startsWith(String prefix):判断该字符是否以prefix开头
- boolean endsWith(String suffix):判断该字符串是否以suffix结尾
public class Sample{
public static void main(String[] args){
String s1 = "abc";
String s2 = "abd";
System.out.println(s1.compareTo(s2));
//结果<0 s1在s2之前 ==0 s1和s2一样 >0 s1在s2之后
System.out.println("abcbc".contains("bcb"));
System.out.println("lala".equals("lala"));
System.out.println("".isEmpty());
System.out.println(s2.isEmpty());
String s3 = "大桥未久.avi";
System.out.println(s3.endsWith(".avi"));
System.out.println(s3.startsWith("大桥未久"));
}
}
修改相关
Integer.parseInt(str) 一种将字符串转数字的方法
- String toLowerCase():将字符串中所有的英文字母全部变为小写
- String toUpperCase():将字符串中所有的英文字母全部变为大写
- String trim():将字符串两端的多余空格删除
- String replace(char oldCh,char newCh):将字符串中oldch字符替换成newCh字符,也可以是字符串
String s4 = "Happy Boy Happy Girl";
System.out.println(s4.toLowerCase());
System.out.println(s4.toUpperCase());
System.out.println(s4);
System.out.println(" 123123 321321 ".trim());
String s5 = "旺财是一只狗,旺财咋叫?旺财来叫一个~";
System.out.println(s5.replace('狗''猫'));
System.out.println(s5.replace("旺财","小强"));
如何删除字符串中左右两端出现的空格(不用tirm)
public class Sample{
public static void main(String[] args){
String str = " 123123123 123234 ";
int l = 0;
int r = str.length() - 1;
while (str.charAt(l) == ' '){
l++;
}
while (str.charAt(r) == ' '){
r--;
}
System.out.println(str.substring(l,r + 1));
}
}
在字符串“abcbcbcbcbcbcbc”中统计“bcb”出现的次数
public class Sample{
public static void main(String[] args){
//非贪心算话 3个
String str1 = "abcbcbcbcbcb";
String str2 = "bcb";
int l = 0;
int j = 0;
int k = 0;
for (l = 0;l < str1.length();l++){
if (str1.charAt(l) == str2.charAt(j)){
j++;
} else {
j = 0;
}
if (j == 3){
j = 0;
k++;
}
}
System.out.println(k);
String temp = str1;
int count = 0;
while (true) {
int index = temp.indexOf(str2);
if (index == -1){
break;
}
count++;
temp = temp.substring(index + str2.length());
}
System.out.println(count);
//贪心算法 5个
String temp1 = str1;
int count1 = 0;
while (true) {
int index = temp1.indexOf(str2);
if (index == -1){
break;
}
count1++;
temp1 = temp1.substring(index + str2.length() - 1);
}
System.out.println(count1);
int count2 = 0;
for (int i = 0;i < str1.length() - str2.length() + 1;i++){
if (str1.charAt(i) == str2.charAt(0)){
if (str2.equals(str1.substring(i,i + str2.length()))){
count2++;
}
}
}
System.out.println(count2);
}
}
查找两个字符串中最长的公共子串
public class Sample{
public static void main(String[] args){
String s1 = "今天我喜欢你";
String s2 = "明天我喜欢你 ";
String temp = "";
for (int i = s2.length();i > 0;i--){
for (int j = 0,r = i - 1;r < s2.length();j++,r++){
temp = s2.substring(j,r+1);
if (s1.contains(temp)){
System.out.println(temp);
return;//直接结束程序
}
}
}
}
}
4.5 Character类
character 他是char基本数据类型的包装类,有这么几个静态方法目前可以使用得到
- static boolean isDigit(char ch):判断字符是否是数字
- static boolean isLetter(char ch):判断字符是否是字母
- static boolean isLetterOrDigit(char ch):判断是否是数字或字母
- static boolean isLowerCase(char ch):判断是否是小写字母
- static boolean isUpperCase(char ch):判断是否是大写字母
- static boolean isSpaceChar(char ch):判断是否空白字母(空格 制表符 回车)
将十六进制转十进制
import java.util.*;
public class Sample{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter:");
String hex = input.nextLine().toUpperCase();
if (hex.length() == 0){
System.out.println("没有任何人输入!");
return;//程序直接结束
}
for(int i = 0;i < hex.length();i++){
char ch = hex.charAt(i);
if(Character.isDigit(ch) || Character.isLetter(ch)){
if (Character.isLetter(ch) && (ch > 'F' || ch < 'A')){
System.out.println("非法字符" + ch);
return;
}
} else {
System.out.println("非法字符" + ch);
return;
}
}
double sum = 0;
int j = 0;
for (int i = hex.length() - 1;i >= 0;i--){
char ch = hex.charAt(i);
if (Character.isDigit(ch)){
sum = sum + (ch - 48) * Math.pow(16,j);
} else {
sum = sum + (ch - 55) * Math.pow(16,j);
}
j++;
}
System.out.println(sum);
}
}
总结
字符转对应的数字
‘0’ 48 ‘A’ 65 ‘a’ 97 将char 转为int类
用字符对应的编码减去一个int类型的整数等于整数(int)
‘0’ - 48 ===》 0
(char)(y + 55) ===》‘A’
同理: =对应的数字也可以转为字符
import java.util.Scanner;
public class Demo59{
/*
39 / 16 == 2 / 7
2 / 16 == 0 /2
*/
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter number:");
int number = input.nextInt();
String sum = "";
while (number != 0) {
int y = number % 16;
if (y < 10){
sum = y + sum;
} else {
sum = (char)(y + 55) +sum;
}
number /= 16;
}
System.out.println(sum);
}
}