Java高级特性 实用类 (二)(第二十四天)

Math() 类(数学算术)

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

方法:
Math.max();//比较两个最大值
Math.min();//比较两个最小值
Math.abs();//求绝对值;负为正,正为正
Math.sin();//返回角度的正弦值

Math.floor();//上舍入;返回最大的(最接近正无穷大) double值,该值小于等于参数,并等于某个整数。

Math.ceil();//下舍入;返回最小的(最接近负无穷大) double值,该值大于等于参数,并等于某个整数。

Math.round();//四舍五入;返回最接近参数的int值

package Math.Demo02;

public class MathDemo01 {

	public static void main(String[] args) {
		//Math类, Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
		
		double pi = Math.PI;
		System.out.println(pi);

		System.out.println(Math.max(100, 200));//比较两个最大值
		System.out.println(Math.min(50, 100));//比较两个最小值
		System.out.println(Math.abs(-20));//求绝对值;负为正,正为正
		System.out.println(Math.sin(0.5));//返回角度的正弦值
		
		System.out.println(Math.floor(9.8));//上舍入;返回最大的(最接近正无穷大) double值,该值小于等于参数,并等于某个整数。
		System.out.println(Math.ceil(9.1));//下舍入;返回最小的(最接近负无穷大) double值,该值大于等于参数,并等于某个整数。
		System.out.println(Math.round(9.8));//四舍五入;返回最接近参数的int值
		
		//随机数方法:Math.random( ):随机返回一个介于0.0(包括)~1.0(不包括)之间的double类型的数据
		double num1 = Math.random();
		System.out.println(num1);
		/*
		 *随机返回一个[num1,num2)之间的int类型的整数(num2>num1)
		 *  int num = (int)(Math.random()*(num2-num1)+num1);
		 */
		/*
		 *枚举有何作用?
		 *列举出某一个属性所需要的正确值,限制赋值范围
		 */
		
	}

}

 Random() 类(随机)

生成随机数的其他方式
  java.util.Random类 

Random rand=new Random(); //创建一个Random对象
for(int i=0;i<20;i++){//随机生成20个随机整数,并显示

         int num=rand.nextInt(10);//返回下一个伪随机数,整型的

System.out.println("第"+(i+1)+"个随机数是:"+num);


随机数方法:Math.random( ):随机返回一个介于0.0(包括)~1.0(不包括)之间的double类型的数据
        double num1 = Math.random();
        System.out.println(num1);

随机返回一个[num1,num2)之间的int类型的整(num2>num1)
       int num = (int)(Math.random()*(num2-num1)+num1);

注意:
用同一个种子值来初始化两个Random 对象,然后用每个对象调用相同的方法,得到的随机数也是相同的

package Math.Demo02;

import java.util.Random;

public class RandomDemo01 {

	public static void main(String[] args) {
		//创建Random类对象
		Random random =new Random();//无参构造创建对象
		int result1= random.nextInt();
		System.out.println(result1);
		System.out.println(random.nextBoolean());
		
		//创建Random类对象
		Random random2 = new Random(10);//有参构造创建对象
		System.out.println(random2.nextInt(10));
		
		//创建Random类对象
		Random random3= new Random();
		System.out.println(random3.nextInt(10));//随机生成一个[0,10)之间的整数
		
		/* 用同一个种子值来初始化两个Random对象,然后用每个对象调用相同的方
		 * 法,得到的随机数也是相同的
		 */
		Random r1= new Random(100);
		Random r2= new Random(100); 
		System.out.println(r1.nextBoolean());
		System.out.println(r2.nextBoolean());	
	}

}

String() 类(字符串)

使用String对象存储字符串
String s = new String();
String s = "Hello World";
String s = new String("Hello World");

String类位于java.lang包中,具有丰富的方法
计算字符串的长度、比较字符串、连接字符串、提取字符串

length()方法:
String类提供了length()方法,确定字符串的长度


StringBuilder类、StringBuffer类:
     建议优先采用StringBuilder类
     主要操作为:
             append() 添加字符串到末端
             insert() 指定点添加字符串
             reverse() 字符反转

package String.Demo03;

public class StringDemo01 {
	
	public static void main(String[] args) {
		
		String string = "sadqfafad";
		
		//获取字符串的长度
		int length=string.length();
		System.out.println("字符串长度:"+length);
		
		/*
		 *获取数组长度:数组名.length
		 *获取集合长度:集合名.size()
		 *获取字符串长度:字符串对象名.length() 
		 */
		
		//比较两个字符串的长度是否相同
		String str1="qwe";
		String str2="qwe";
		System.out.println(str1.equals(str2));//值为:true
		System.out.println(str1==str2);//值为:true
		
		String str3=new String("asd");
		String str4=new String("asd");
		System.out.println(str3.equals(str4));//值为:true
		System.out.println(str3==str4);//false
		
		//其他比较字符串的方法
		String str5="qwert";
		String str6="Qwert";
		//区分大小写进行比较
		System.out.println(str5.equals(str6));//大小写比较:
		//equalsIgnoreCase()不区分大小写进行比较
		System.out.println(str5.equalsIgnoreCase(str6));//大小写比较
		
		//toLowerCase()大写转小写
		String result = str6.toLowerCase();
		System.out.println(str6);//Qwert //字符串对象调用方法后,不会改变自身的内容,相当于是对它的复制品进行了修改
		System.out.println(result);//qwert
		
		System.out.println(str5.toUpperCase());//QWERT //返回结果值,结果值全部大写
		
	}
}

---------------------------------------------------------------------------------------
package StringBuffer.Demo04;

import java.util.Scanner;

public class StringBufferDemo02 {

	public static void main(String[] args) {
		
		//如:输入1234567890
		Scanner sc = new Scanner(System.in);
		System.out.println("输入一串数字");
		String num =sc.next();
		StringBuffer sb = new StringBuffer(num);
		for(int i=sb.length()-3;i>0;i-=3){
			sb.insert(i, " ");
		}
		System.out.println(sb);//结果:1 234 567 890
		
		
		
		StringBuilder sbl=new StringBuilder(num);
		System.out.println(sbl.reverse());
		/*
		 * StringBuilder类:主要操作为append(添加到末端)和insert(指定点添加)方法,
		 *   reverse() 字符反转
		 *   
		 * StringBuffer类:
		 * 
		 * 建议优先采用StringBuilder类
		 */
	}

}
package String.Demo03;

import java.util.Arrays;

public class StringDemo03 {

	public static void main(String[] args) {
		//字符串常用提取方法
		String str1="sdfgahjklxcvbnma";
		
		//public int indexOf(int ch)搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
		//public int indexOf(String value)搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
		int index = str1.indexOf(97);//ASCII码97对应的是a,a下标为4
		System.out.println(index);
		System.out.println(str1.indexOf("hj"));//从前往后查找字符;返回下标值:5
		
		//public int lastIndexOf(int ch)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
		//public int lastIndexOf(String value)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
		System.out.println(str1.lastIndexOf(97));//从后往前查找字符;返回下标值:15
		System.out.println(str1.lastIndexOf("nma"));//从后往前查找字符;返回下标值:13
		
		//public String substring(int index)提取从位置索引开始的字符串到末尾部分
		System.out.println(str1);//sdfgahjklxcvbnma
		System.out.println(str1.substring(3));//截取下标值(包括下标值)之后的字符串:gahjklxcvbnma
		
		//public String substring(int beginindex, int endindex)提取beginindex(包括)和endindex(不包括)之间的字符串部分
		System.out.println(str1);//sdfgahjklxcvbnma
		System.out.println(str1.substring(3,6 ));//gah
		
		//public String trim()返回一个前后不含任何空格的调用字符串的副本
		String str2 = "       wdad    dmjl      ";
		System.out.println(str2);//"       wdad    dmjl      "
		System.out.println(str2.trim());//trim()去除字符串首尾空格:"wdad    dmjl"

		//字符串拆分方法
		String str3 = "长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
		String[] strs = str3.split(" ");
		for (String string : strs) {
			System.out.println(string);
		}
		
		String str4 = "我爱你就像老鼠爱大米";
		String[] strs2=str4.split("爱");//遇"爱"拆分:[我, 你就像老鼠, 大米]
		System.out.println(Arrays.toString(strs2));
		
		//判断字符串是否以指定内容结尾
		String str5 = "HelloWorld.java";
		System.out.println(str5.endsWith(".java"));//是返回true,否则false
		//判断字符串是否以指定内容开头
		System.out.println(str5.startsWith("He"));
		
		//将字符串变成byte类型的数组
		byte[] bytes=str5.getBytes();
		for (byte b : bytes) {
			System.out.print((char)b);//转成字符型char
		}
	}

}

equals() 方法(比较)

equals()方法:
String类提供了equals( )方法,比较存储在两个字符串对象的内容是否一致

==:判断两个字符串在内存中的地址,即判断是否是同一个字符串对象

字符串比较的其他方法:
使用equalsIgnoreCase()方法 
使用toLowerCase()方法
使用toUpperCase()方法

package String.Demo03;

public class StringDemo01 {
	
	public static void main(String[] args) {
		
		String string = "sadqfafad";
		
		//获取字符串的长度
		int length=string.length();
		System.out.println("字符串长度:"+length);
		
		/*
		 *获取数组长度:数组名.length
		 *获取集合长度:集合名.size()
		 *获取字符串长度:字符串对象名.length() 
		 */
		
		//比较两个字符串的长度是否相同
		String str1="qwe";
		String str2="qwe";
		System.out.println(str1.equals(str2));//值为:true
		System.out.println(str1==str2);//值为:true
		
		String str3=new String("asd");
		String str4=new String("asd");
		System.out.println(str3.equals(str4));//值为:true
		System.out.println(str3==str4);//false
		
		//其他比较字符串的方法
		String str5="qwert";
		String str6="Qwert";
		//区分大小写进行比较
		System.out.println(str5.equals(str6));//大小写比较:
		//equalsIgnoreCase()不区分大小写进行比较
		System.out.println(str5.equalsIgnoreCase(str6));//大小写比较
		
		//toLowerCase()大写转小写
		String result = str6.toLowerCase();
		System.out.println(str6);//Qwert //字符串对象调用方法后,不会改变自身的内容,相当于是对它的复制品进行了修改
		System.out.println(result);//qwert
		
		System.out.println(str5.toUpperCase());//QWERT //返回结果值,结果值全部大写
		
	}
}

字符串连接

方法1:使用“+”

方法2:使用String类的concat()方法
String str1="qwert";
String str2="tyuu";
String result= str1.concat(str2);
System.out.println(result);

SimpleDateForma()  类(时间、日期)

"yyyy-MM-d HH:mm:ss E "中国时区格式

package SimpleDateFormat.Demo05;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DataDemo01 {

	public static void main(String[] args) {
		//日期时间
		//创建Date类对象
		Date date = new Date();
		System.out.println(date);//Thu Aug 18 01:45:29 CST 2022
		
		//Date类中很多方法都已过时,使用Calendar类替代
		/*System.out.println(date.getYear()+1900);
		System.out.println(date.getMonth()+1);*/
		
		//中国时间格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-d HH:mm:ss E D");
		String str = sdf.format(date);
		System.out.println(str);//2022-08-18 02:00:22 星期四 230(8月十八号是一年中的第230天)
		
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值