StringBuffer的使用
定义一个 String Buffer 类对象,然后通过 append()方法向对象中添加 26 个小写字母,要求每次只添加一次,共添加 26 次,然后按照逆序的方式输出,并且可以删除前 5 个字符。
本操作主要是训练 String Buffer 类中的处理方法,因为 String Buffer 的主要特点是内容允许修改。
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer();
for(int x='a';x<='z';x++){
buf.append((char)x);
}
buf.reverse().delete(0,5);
System.out.println(buf);
}
}
随机数组
利用 Random 类产生 5 个 1~30 之间(包括 1 和 30)的随机整数。
分析:Random 产生随机数的操作之中会包含有数字 0,所以此时不应该存在有数字 0 的问题。
import java.util.Arrays;
import java.util.Random;
class NumberFactory{
public static Random random =new Random();
public static int [] create(int len){
int [] data=new int[len];
int foot=0;
while(foot<len){
int num=random.nextInt(30);
if(num!=0){
data[foot++]=num;
}
}
return data;
}
}
public class RandomDemo {
public static void main(String[] args) {
System.out.println(Arrays.toString(NumberFactory.create(5)));
}
}
Email验证
输入一个Emial地址,然后用正则表达式验证该Email地址是否正确
对于此时的输入可以通过命令参数实现数据的收入,如果要想进行验证,最好的做法是设置一个单独的验证处理类。
class Validator{
private Validator(){}
public static boolean isEmail(String email){
if(email==null||"".equals(email)){
return false;
}
String regex="\\w+@\\w+\\.\\w+";
return email.matches(email);
}
}
public class JavaAPIDemo {
public static void main(String[] args) {
if(args.length!=1){
System.out.println("请初始化参数");
System.exit(1);
}
String email=args[0];
if( Validator.isEmail(email)){
System.out.println("Email地址格式正确");
}else{
System.out.println("Email地址错误");
}
}
}
扔硬币
编写程序用0-1之间的随机数来模拟扔硬币实验,统计扔1000之后正反面的次数并输出
import java.util.Random;
class Coin{//模拟硬币操作
private int front;
private int back;
private Random random=new Random();
public void throwCoin(int num){
for (int i = 0; i < num; i++) {
int temp = random.nextInt(2);
if(temp==0){
this.front++;
}else{
this.back++;
}
}
}
public int getFront(){
return this.front;
}
public int getBack(){
return this.back;
}
}
public class JavaAPIDemo {
public static void main(String[] args) {
Coin coin = new Coin();
coin.throwCoin(1000);
System.out.println("正面的次数"+coin.getFront());
System.out.println("反面的次数"+coin.getBack());
}
}
IP验证
编写正则表达式,判断给定的是否是一个合法的正则表达式
class Validator{
public static boolean validateIP(String ip){
if(ip==null||"".equals(ip)){
return false;
}
String regex="([12]?[0-9][0-9]\\.){3}[12]?[0-9][0-9]";
if(ip.matches(regex)){//验证通过还需要对IP地址进行拆分处理
String result[]=ip.split("\\.");
for (int i = 0; i < result.length; i++) {
int temp = Integer.parseInt(result[i]);
if(temp>255){
return false;
}
}
}
return true;
}
}
public class JavaAPIDemo {
public static void main(String[] args) {
if(Validator.validateIP("192.111.111.111")){
System.out.println("IP验证通过");
}else{
System.out.println("IP验证失败");
}
}
}
HTML拆分
给定下面的HTML代码
<font face="Arial, Serif" size="+2" color="red">
要求对内容进行拆分,拆分之后的结果为:
face Arial,Serif
size +2
color red
分析:对于此时的操作最简单的做法就是进行分组处理
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaAPIDemo {
public static void main(String[] args) {
String str=" <font face=\"Arial,Serif\" size=\"+2\" color=\"red\">";
String regex="\\w+=\"[a-zA-Z0-9,\\+]+\"";
Matcher mathcer= Pattern.compile(regex).matcher(str);
while(mathcer.find()){
String temp=mathcer.group();
String [] result=temp.split("=");
System.out.println(result[0]+"\t"+result[1].replaceAll("\"",""));
}
}
}
国家代码
编写程序实现国际化应用,从命令行输入国家的代号,例如: 1表示中国,2表示美国,然后根据输入代号不同调用不同的资源文件显示信息。
分析: 本程序的实现肯定要通过Locale类的对象类指定区域,随后利用ResourceBundle类加载资源文件,而继续使用初始化参数形式来完成。
首先要定义中英文的资源文件
import java.util.Locale;
import java.util.ResourceBundle;
class MessagesUtil{
public static final int CHINA=1;
public static final int USA=2;
public static final String KEY="info";
public static final String BASENAME="com.woo.message.Message";
public String getMessage(int num){
Locale loc=this.getLocale(num);
if(loc==null){
return "nothing";
}
else{
return ResourceBundle.getBundle(BASENAME,loc).getString(KEY);
}
}
private Locale getLocale(int num){
switch(num){
case CHINA:
return new Locale("zh","CN");
case USA:
return new Locale("en","US");
default:
return null;
}
}
}
public class JavaAPIDemo {
public static void main(String[] args) {
if(args.length!=1){
System.out.println("请输入参数");
System.exit(1);
}
int chose=Integer.parseInt(args[0]);
System.out.println( new MessagesUtil().getMessage(chose));
}
}
学生信息比较
按照“姓名:年龄:成绩|姓名:年龄:成绩”的格式定义字符串“张三:21:98|李四:22:89|王五20:70,要求将每组值分别保存在 Student 对象之中,并对这些对象进行排序,排序的原则为:按照成绩由高到低排序,如果成绩相等,则按照年龄由低到高排序。
本程序最典型的做法是直接利用比较器来完成处理,如果不使用比较器也可以完成,相当于自己采用冒泡方式进行排列组合,使用了比较器就可以利用Arrays类做处理
import java.util.Arrays;
public class JavaAPIDemo {
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 i = 0; i < result.length; i++) {
String temp[]=result[i].split(":");
student[i]=new Student(temp[0],Integer.parseInt(temp[1]),Double.parseDouble(temp[2]));
}
System.out.println(Arrays.toString(student));
}
}
class Student implements Comparable<Student>{
private String name;
private int age;
private double score;
public Student(String name,int age, double score){
this.name=name;
this.age=age;
this.score=score;
}
@Override
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;
}
}