Java笔记整理(二)

本文深入讲解了Java中的Math类、Scanner类、Random类和String类的功能及使用方法,包括数学运算、输入处理、随机数生成及字符串操作等关键知识点。

第四章:Java常用类

1.Math类:

Math类是用于数学计算的一个工具类

对于工具类而言,里面的大部分成员都是静态的static

对于Math类有:
(1)自带常量:

自带常量描述
static double E自然对数
static double PI圆周率

(2)三角函数方法:

方法描述
static double sin(double a)正弦函数
static double cos(double a)余弦函数
static double tan(double a)正切函数
static double toDegrees(double a)将弧度转角度
static double toRadians(double angles)将角度转弧度
static double asin(double a)反正弦函数
static double acos(double a)反余弦函数
static double atan(double a)反正切函数

sin、cos、tan的参数都是以弧度为单位的角度 ;
asin的返回值是-π/2~π/2的一个弧度值;
acos的返回值0~π之间;

例如:

Math. sin(Math. toRadians(270))//返回-1.0
Math. toDegrees (Math.PI / 2)//返回90.0
Math. toRadians(30)//返回0.5236 (π/6)
Math.sin(0)//返回0.0
Math. cos(Math.PI / 6)//返回0. 866
Math.asin(0.5)//返回0. 523598333(π/6)
Math.atan(1.0)//返回0. 785398 (π/4)

(3)取整方法:

方法描述
static double ceil(double a)向上取整
static double floor(double a)向下取整
static long round(double a)四舍五入

例如:

Math.cei1(2.1)返回3.0
Math.floor(-2.0)返回-2
Math.floor(-2.1)返回-3
Math.rint(2.1)返回2.0
Math.rint(-2.1)返回-2.0
Math.rint(2.5)返回2.0
Math.rint(4.5)返回4.0
Math.round(2.0)返回2// Returns long
Math.round(2.6f)返回3//Returns int

(4)指数函数:

方法描述
static double pow(double a, double b)求a的b次幂
static double sqrt(double a)求a的平方根
static double cbrt(double a)求a的立方根
static double pow(a,b)求a的b次方
static double exp(x)求e的x次方

例如:

Math.exp(3.5)//返回33. 11545
Math.1og(3.5)//返回1.25276
Math.sgrt (4)//返回2.0
Math. sqrt (10.5)//返回3.24

(5)其他方法:

方法描述
static double abs(double a)求a的绝对值
static double hypot(double deltX, double deltY)返回 两点间距离
static double max(a,b)返回a和b之间的最大值
static double min(a,b)返回a和b之间的最小值
static double random()返回[0,1)之间的随机小数

2.Scanner类:

主要用于负责数据输入的类,底层是和IO流相关。

类型描述
String next()获取直到遇到空格为止的一个字符串
String nextLine()获取直到遇到回车为止的一个字符串
int nextInt()获取下一个整数 byte short long
double nextDouble()获取直到是双精度浮点数
boolean nextBoolean()“随机生成器”返回下一个伪布尔值
float nextFloat()获取直到是浮点数

3.Random类

主要用于产生随机数

类型描述
boolean nextBoolean()随机产生布尔类型值
double nextDouble()随机生成0.0~1.0之间的小数
double nextInt()随机生成一个整数0~2^32的整数
double nextInt(n)随机生成一个整数[0,n)的整数

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"
}
}

(1)获取相关

方法描述
char charAt(int index)获取指定角标index处的字符
int indexOf(int ch)获取指定字符(编码)在字符串中第一次(从左到右)出现的地方返回的是角标如果是-1 表示不存在
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 beginIndex, int endIndex)截取一段子字符串,从beginIndex开始到endIndex(不包含)

例如:
String message = “Welcome to Java”;
String message = message. substring(0,11) + “HTML” ;
例如:

"We1come to Java" .indexOf("java",5) //returns -1.
"Welcome to Java" .lastIndexOf('o')//returns 9.
"Welcome to Java". lastIndexOf("come") //returns 3.
"Welcome to Java". 1astIndexOf("Java")//returns 11.

(2)判断相关

方法描述
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 suix)判断该字符串是否已suix结尾

例如:

"Welcome to Java" . startsWith("We")//returns true.
"We1come to Java" . startsWith("we")// returns false.
"We1come to Java" . endsWith("va")// returns true.
"We1come to Java" . startsWith("we") //returns false.
"Welcome to Java" . contains("to")// returns true.
"We1come to Java" . contains("To")// returms false.

(3)修改相关

方法描述
String toLowerCase()将字符串中所有的英文字母全部变为小写
String toUpperCase()将字符串中所有的英文字母全部变为大写
String trim()将字符串中两端的多余空格进行删除
String replace(char oldCh,char newCh)将字符串中oldCh字符替换成newCh字符

例如:在字符串"abcbcbcbcbcbc"中统计"bcb"出现的次数

//
public class Sample {
public static void main(String[] args) {
//最好的算法KMP算法
String s1 = "abcbcbcbcbcbc";
String s2 = "bcb";
//贪心算法 5个
int count = 0;
for (int i = 0; i < s1.length() - s2.length() + 1; i++) {
if (s1.charAt(i) == s2.charAt(0)) {
if (s2.equals(s1.substring(i,i + s2.length()))) {
count++;
}
}
}
System.out.println(count);//非贪心算法 3个
/*
String temp = s1;
int count = 0;
while (true) {
int index = temp.indexOf(s2);
if (index == -1) {
break;
}
count++;
temp = temp.substring(index + s2.length());
}
System.out.println(count);
*/
}
}//非贪心算法 3个

4.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.Scanner;
public class Demo54{
    public static void main(String[] args){
        Scanner input =new Scanner(System.in);
        System.out.print(" 1  3  5  7\n 9 11 13 15\n17 19 21 23\n25 27 29 31");
        System.out.println("\n你的日期在这些数字里面吗:y(1) / n(0)");
        int answer = input.nextInt();
        int day = 0;
        if(answer == 1){
            day = day + 1; 
        }  
        System.out.print(" 2  3  6  7\n10 11 14 15\n18 19 22 23\n26 27 30 31");
        System.out.println("你的日期在这些数字里面吗:y(1) / n(0)");
        answer = input.nextInt();  
        if(answer == 1){
            day = day +2;
        }
        System.out.print(" 4  5  6  7\n12 13 14 15\n20 21 22 23\n28 29 30 31");
        System.out.println("你的日期在这些数字里面吗:y(1) / n(0)");
        answer = input.nextInt();
        if(answer == 1){
            day = day +4;
        } 
        System.out.print(" 8  9  10  11\n12 13 14 15\n24 25 26 27\n28 29 30 31");
        System.out.println("你的日期在这些数字里面吗:y(1) / n(0)");
        answer = input.nextInt();
        if(answer == 1){
            day = day +8;
        }
        System.out.print("16 17 18 19\n20 21 22 23\n24 25 26 27\n28 29 30 31");
        System.out.println("你的日期在这些数字里面吗:y(1) / n(0)");
        answer = input.nextInt();
        if(answer == 1){
        day = day + 16;
    }
        System.out.println("\n 你的生日是" + day + "号");
}

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

风儿吹吹吹

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

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

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

打赏作者

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

抵扣说明:

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

余额充值