java期末冲刺!常用类,涉及字符串类String和StringBuffer、Math类和Random类

一、考察范围:

字符串处理、生成随机数

1.String类

String 对象是不可变的。一旦创建了一个 String 对象,其内容就不能更改。如果对 String 进行任何操作(如拼接、替换等),都会生成一个新的 String 对象

(1)字符串的声明与创建的两种方式:

//方式一:使用字符常量直接初始化一个String对象

String 变量名 = 字符串;

String s1 = null        //设置s1为空

String s2 = “”           //设置s1为空字符串

String s3 = “zhou”   //设置s1为“zhou”

//方式二:调用String类的构造方法初始化字符串常量

String 变量名 = new String(字符串);

(2)String类常用方法(重点:*):

*String subString(int bege,int end)     //bege和end指索引,[bege,ebd)

*char[] toCharArry()      //将当前字符串转为字符数组

*char charAt(int index)//返回当前字符串中index位置上的字符

*boolean equal(Object obj)      //比较obj与当前字符串是否相等

boolean contains(CharSequence cs)   //判断当前字符串中是否包含字符序列cs

int inIndex(int ch)       //返回指定字符ch当前在字符串中出现第一次的位置(索引)

*int lastIndex(int ch)    //返回指定字符ch当前在字符串中出现最后一次的位置(索引)
String toUpperCase()

String toLowerCase() 

2.StringBuffer类

区别于String类,StringBuffer 是可变的。可以直接修改它的内容,而不生成新的对象。这使得它在频繁修改字符串的情况下(如循环拼接)更加高效

(1)定义一个字符缓冲区:

StringBuffer sb = new StringBuffer();

sb.append(字符串); // 接着在空字符串末尾添加字符串

//一步到位即StringBuffer sb = new StringBuffer(字符串);

(2)StringBuffer类的常用方法(重点:*)

StringBuffer append(char c)

StringBuffer insert(int offer,String str)

StringBuffer deleteCharAt(int index)

StringBuffer ddelete(int start, int end,String str)

StringBuffer reverse()  //反转字符串:“abc”->"cba"

StringBuffer toString()//转为字符串类型

*String subString(int bege,int end)

3.Random类

(1)创建Random对象:

Random randon = new Random(); //不传入种子

Random randon = new Random(100); //种子数为100

(2)Random类的常用方法:用于生成各种类型的随机数

Boolean nextBooklean()

double nextDouble()

float nextFloat()

long nextLong()

Int nextInt()

Int nextInt(int n) //生成[0,n)区间的int类型的随机数

4.Math类

(1)Math类中的所有方法都是静态方法,可以直接通过类名调用Math类中的方法,无需创建Math对象。

(2)Math类常用方法

sqrt(double a) //a的平方根

max(double a,double b)

mix(double a,double b)

abs(double a)

random() //用于生成一个大于或等于0.0且小于1.0的随机数         [0.0,1.0)

5.用这些类前记得导包:import java.util.*

二、实验题目

1.已知某学生的学号为“2024090312”,学号的组成成分如下表所示:

2024

09

03

12

入学年份

系代码

班级代码

班内代码

请用字符串的方式,计算该学生的入学年份,所在系代码,班级代码和班内编号,分别保存在字符串str1,str2,str3,str4中,并打印输出。

2.用字符串方式,统计出某一段文字中大写字母、小写字母、数字、空格出现的次数。

3.判断某字符串下标为奇数的字符所组成的字符串与下标为偶数的字符所组成的字符串是否相等。

4.编程实现随机生成验证码

1随机生成6位数字组成的验证码

2随机生成6位由数字和英文字母组成的验证码

三、解题代码

第一题

方法一

import java.lang.*;
import java.util.*;
public class Zifutiqu {
	public static void main(String[] args) {
		String s1=new String("My Name is Wnn.My Telephone 2024090312");
		Scanner input=new Scanner(System.in);
	    s1=input.next();
	    System.out.println(s1); 
	    StringBuffer str1=new StringBuffer( );
		StringBuffer str2=new StringBuffer( );
		StringBuffer str3=new StringBuffer( );
		StringBuffer str4=new StringBuffer( );
		str1.append(s1.substring(0,4));
		str2.append(s1.substring(4,6));
		str3.append(s1.substring(6,8));
		str4.append(s1.substring(8,10));
		System.out.println("入学年份为:"+str1);
		System.out.println("系代码为:"+str2);
		System.out.println("班级代号为:"+str3);
		System.out.println("班内编号为:"+str4);

    }
}

方法二

public class Student {
    public static void main(String[] args){
        String id="2024090312";
        String str1=id.substring(0,4);
        String str2=id.substring(4,6);
        String str3=id.substring(6,8);
        String str4=id.substring(8);
        System.out.println("入学年份为:"+str1);
        System.out.println("系代码为:"+str2);
        System.out.println("班级代号为:"+str3);
        System.out.println("班内编号为:"+str4);
    }
}

第二题

import java.lang.*;
import java.util.*;
public class Zifucount {
	public static void main(String[] args) {
		 Scanner input=new Scanner(System.in);
		 s1=input.next();
		 System.out.println(s1); 
		 int num1=0,num2=0,num3=0,space=0;
		 for(int i=0;i<s1.length();i++)
		   if(s1.charAt(i)>='a'&&s1.charAt(i)<='z')
			 num1++;
		   else if(s1.charAt(i)>='A'&&s1.charAt(i)<='Z')
			 num2++;
		   else if(s1.charAt(i)>='0'&&s1.charAt(i)<='9')
			 num3++;
		   else if(s1.charAt(i)==' ')
			  space++;
		   System.out.println("小写字母数为:"+num1);
		   System.out.println("大写字母数为:"+num2);
		   System.out.println("数字字符数为:"+num3);
		   System.out.println("空格个数为:"+space);
	}
}

第三题

方法一

import java.util.*;
public class Judge {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String str  = scanner.next();
        char[] s=str.toCharArray();
        String s1="";
        String s2="";
        for(int i=0;i<s.length/2;i++){
            if(2*i<s.length) {
                s1+=s[2 * i];
            }
            if(2*i+1<s.length){
                s2 += s[2 * i + 1];
            }
        }
        System.out.println("s1="+s1+" S2="+s2);
        if(s1.equals(s2)){//比较字符串: 使用 s1.equals(s2) 而不是 s1 == s2 来比较两个字符串的内容
                    // 使用 == 比较两个字符串对象时,它比较的是这两个对象在内存中的引用地址
            System.out.print("yes");
        }else {
            System.out.print("no");
        }
    }
}

方法二

import java.lang.*;
import java.util.*;
public class Pandeng {
	public static void main(String[] args) {
		String s1=new String();
		Scanner input=new Scanner(System.in);
		s1=input.next();
		System.out.println(s1); 
		StringBuffer s2=new StringBuffer( );
		StringBuffer s3=new StringBuffer( );
		for(int i=0;i<s1.length();i++)
		    if(i%2==0)
		        s2.append(s1.charAt(i));
		    else
		        s3.append(s1.charAt(i));
        System.out.println(s2);
        System.out.println(s3);
    	if(s2.toString.equals(s3.toString))
		    System.out.println("两串相等");
	    else 
		    System.out.println("两串不相等");
	}
}

第四题

import java.util.Random;
public class YzmRandom1 {
    public static void main(String args[]) {
        Random random = new Random();
        System.out.print("随机生成6位数字组成的验证码:");
        for (int i = 0; i < 6; i++) {
            System.out.print(random.nextInt(9));
        }
    }
}

第五题

import java.util.Random;
public class PasswordGenerator {
    public static void main(String[] args) {
        String alphanumer = password(6);
        System.out.println("随机生成的字母数字验证码: " + alphanumer);
    }
    public static String password(int length) {
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; // 包含字母和数字
        Random random = new Random();
        StringBuilder captcha = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(chars.length());//最大索引值小于chars.length
            captcha.append(chars.charAt(randomIndex)); // 随机选择字符
        }
        return captcha.toString();
    }
}

说明:复习内容基于本人实际学习情况,发布文章仅用于记录。如有错误,欢迎纠正。

第四部分:常用类

实验9、实验10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值