Java周测题

1、将字符串「a-b-c-d-e-f」按 「-」 切割,找到 「c」字符,替换为大写,然后倒序输出 「f-e-d-C-b-a」。

public class Test1 {
	public static void main(String[] args) {
		String str = "a-b-c-d-e-f";

		String[] strs = str.split("-");

		// 循环遍历,小写字母变大写
		for (int i = 0; i < strs.length; i++) {
			String s = strs[i];
			if (s.equals("c")) {
				strs[i] = s.toUpperCase();
			}
		}
		String temp = "";
		for (int i = strs.length - 1; i >= 0; i--) {
			temp += strs[i] + "-";
		}
		// 去除最后一个短横
		System.out.println(temp.substring(0, temp.length()-1));
	}
}

2.定义学生类(包含学号、姓名、年龄),将你所在小组组员添加到一个集合中,并按学号排序后输出。

public class Student implements Comparable<Student> {
	private int code;
	private String name;
	private int age;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	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 Student(int code, String name, int age) {
		super();
		this.code = code;
		this.name = name;
		this.age = age;
	}

	@Override
	public int compareTo(Student o) {
		int flag;
		if (this.code > o.code) {
			flag = -1;
		} else if (this.code == o.code) {
			flag = 0;
		} else {
			flag = 1;
		}
		return flag;
	}

	@Override
	public String toString() {
		return "Student [code=" + code + ", name=" + name + ", age=" + age
				+ "]";
	}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test2 {
	public static void main(String[] args) {
		List<Student> teams = new ArrayList<Student>();
		// 新建学生对象
		Student stu1 = new Student(1, "Tom", 15);
		Student stu2 = new Student(3, "Jack", 13);
		Student stu3 = new Student(2, "Helen", 18);
		Student stu4 = new Student(4, "May", 12);
		// 向集合添加学生对象
		teams.add(stu1);
		teams.add(stu2);
		teams.add(stu3);
		teams.add(stu4);
		// 按学号倒叙排列
		Collections.sort(teams);
		// 遍历学生
		for (Student stu : teams) {
			System.out.println(stu);
		}
	}
}

3、紧接第二题,用单例设计一个服务类,并定义一个方法,可以随机抽取集合中的某个学生对象,并打印输出。

import java.util.ArrayList;
import java.util.List;

public class Test3 {
	public static void main(String[] args) {
		List<Student> teams = new ArrayList<Student>();
		Student stu1 = new Student(1, "Tom", 15);
		Student stu2 = new Student(3, "Jack", 13);
		Student stu3 = new Student(2, "Helen", 18);
		Student stu4 = new Student(4, "May", 12);
		teams.add(stu1);
		teams.add(stu2);
		teams.add(stu3);
		teams.add(stu4);

		// 通过静态方法获取对象
		Service service = Service.getInstance();

		System.out.println("==========随机抽取学生=============");
		Student randomStu = service.randomStu(teams);
		System.out.println(randomStu);
	}
}

import java.util.List;

public class Service {
	private static Service service = new Service();
	
	private Service() {
		
	}
	
	public static Service getInstance() {
		return service;
	}
	
	public Student randomStu(List<Student> stus) {
		if (null != stus && !stus.isEmpty()) {
			return stus.get((int)(Math.random() * stus.size()));
		} else {
			return null;
		}
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值