Java 常用类库的应用

一、实验目的

1、掌握StringBuffer 与String 的区别。

2、掌握日期操作类。

3、掌握比较器及其基本原理。

二、实验内容与结果

1、定义一个StringBuffer 方法对象,然后通过append()方法向对象中添加 26 个小写字母,要求每次只添加一次,共添加 26 次,然后按照逆序的方式输出,并且可以删除前 5 个字符。

代码如下:

package demo9;

public class shiyan91 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StringBuffer str = new StringBuffer();
		char ch = 'a';
		for(int i=0;i<25;i++) {
			str.append(ch);
			ch = (char)(ch + 1);
		}
		str.append(ch);
		System.out.println("添加 26 个小写字母:"+str);
		str.reverse();
		System.out.println("逆序输出:"+str);
		str.delete(0, 5);
		System.out.println("删除前五个小写字母:"+str);
	}

}

实验结果:

 2、利用Random 类产生 5 个 1~30 之间(包括 1 和 30)的随机整数。

代码如下:

package demo9;
import java.util.Random;
public class shiyan92 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Random num = new Random();
		for(int i = 0 ; i <5 ; i++) {
			System.out.println("1-30之间的5个随机数:"+num.nextInt(30));
		}
	}

}

实验结果:

 3、输入一个Email 地址,然后使用正则表达式验证该Email 地址是否正确。

代码如下:

package demo9;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class shiyan93 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str="输入邮箱";
		String pat="\\w+@\\w+\\.\\w+";//验证邮箱格式
		Pattern p=Pattern.compile(pat);
		Matcher m=p.matcher(str);
		if(m.matches()){
		System.out.println("邮箱正确!");
		}else{
		System.out.println("邮箱不正确!");
		}
	}

}

实验结果:

 

 4、编写程序,用 0~1 之间的随机数来模拟扔硬币实验,统计扔 1000 次后出现正反面的次数并输出。

代码如下:

package demo9;
import java.util.Random;
public class shiyan94 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Random IS = new Random();
		int a=0,b=0;
		for(int i = 0 ; i < 1000 ; i++) {
			IS.nextInt(2);
			if(IS.nextInt(2)%2==0) {
				a++;//正面次数
			}else {
				b++;//反面次数
			}
		}
		System.out.println("正面次数为:"+a+"\n反面次数为:"+b);
	}

}

实验结果:

 5、按照“姓名:年龄:成绩|姓名:年龄:成绩”的格式定义字符串“张三:21:98|李四:22:89| 王五:20:70”,要求将每组值分别保存在 Student 对象之中,并对这些对象进行排序,排序的原则为:按照成绩由高到低排序,如果成绩相等,则按照年龄由低到高排序。

代码如下:

package demo9;

import java.util.Arrays;
class Student implements Comparable<Student> {
    private String name;
    private int age;
    private double score;
    public Student(String name, int age, double score) {
    	super();
        this.name = name;
        this.age = age;
        this.score = score;
    }
    public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
    public int compareTo(Student stu) {
        if (this.score < stu.score) {
            return 1;
        } else if (this.score > stu.score) {
            return -1;
        } else {
            return this.age - stu.age;
        }
    }
    public String toString() {
        return "学生信息 [姓名" + this.name + "、年龄:" + this.age + "、成绩:" + this.score + "]\n";
    }
}
public class shiyan95 {
    public static void main(String[] args) {
        String input = "张三:21:98|李四:22:89|王五:20:70";
        String result[] = input.split("\\|");
        Student student[] = new Student[result.length];
        for (int x = 0; x < result.length; x++) {
            String temp[] = result[x].split(":");
            student[x] = new Student(temp[0], Integer.parseInt(temp[1]), Double.parseDouble(temp[2]));
        }
        Arrays.sort(student);
        System.out.println(Arrays.toString(student));
    }
}

实验结果:

 

 6、给定下面的 HTML 代码:

<font face=”Arial,Serif” size=”+2” color=”red”>

要求对内容进行拆分

代码如下:

package demo9;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class shiyan96 {
    public static void main(String[] args) {
        String str = "<font Face=\"Arial,Serif\" size=\"+2\" color=\"red\">";
        String regex = "\\w+=\"[a-zA-Z0-9+,]+\"";
        Matcher matcher = Pattern.compile(regex).matcher(str);
        while (matcher.find()) {
            String temp = matcher.group(0);
            String result[] = temp.split("=");
            System.out.println(result[0] + " " + result[1].replaceAll("\"", ""));
        }
    }
}

拆分之后的结果是:

Face Arial Serif

size +2

color red

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值