【Java基础测试题】2023千锋Java基础阶段测试编程题

第一题

第一题

public static void main(String[] args) {

	System.out.println(sum(100));

}

public static int sum(int n) {
	if (n == 1) {
		return 1;
	} else {
		return sum(n - 1) + n;
	}
}

第二题

第二题

public static void main(String[] args) {

	List<String> list = Arrays.asList("tom", "jack", "marry", "vicky");

	for (String str : list) {
		if (str.contains("ck")) {
			str = str.toUpperCase();
			str += "@1000phone.com";
		}
		System.out.println(str);
	}
}

第三题

第三题

public class Product {

	private Integer id;

	private String name;

	private Double price;

	private String message;

	public Product() {
	}

	public Product(Integer id, String name, Double price, String message) {
		this.id = id;
		this.name = name;
		this.price = price;
		this.message = message;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Product other = (Product) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "编号:" + id + ", 名称:" + name + ", 价格:" + price + ", 详细描述:" + message;
	}

}
public static void main(String[] args) {

	List<Product> list = new ArrayList<>();

	list.add(new Product(23001, "产品1", 5500.0, "描述--"));
	list.add(new Product(23002, "产品2", 3500.0, "描述--"));
	list.add(new Product(23003, "产品3", 4500.0, "描述--"));

	list.sort(new Comparator<Product>() {

		@Override
		public int compare(Product o1, Product o2) {
			int res = o1.getPrice().compareTo(o2.getPrice());
			return res;
		}
	});

	/**
	 * 其他写法:
	 * ① list.sort((o1, o2) -> o1.getPrice().compareTo(o2.getPrice()));
	 * 
	 * ② list.stream().sorted((o1, o2) ->
	 * 	o1.getPrice().compareTo(o2.getPrice())).forEach(System.out::println);
	 */

	for (Product p : list) {
		System.out.println(p);
	}
}

第四题

第四题

public static void main(String[] args) throws IOException {

	Map<Integer, Product> map = new HashMap<>();

	File file = new File("E:\\QQdown\\test.txt");

	FileReader reader = new FileReader(file);

	BufferedReader br = new BufferedReader(reader);

	String str;
	while ((str = br.readLine()) != null) {
		String[] arr = str.split("/");

		map.put(Integer.parseInt(arr[0]),
				new Product(Integer.parseInt(arr[0]), arr[1], Double.parseDouble(arr[2]), arr[3]));
	}

	Set<Entry<Integer, Product>> set = map.entrySet();

	set.stream().sorted((o1, o2) -> o2.getKey().compareTo(o1.getKey()))
			.forEach(t -> System.out.println("Key:" + t.getKey() + " " + t.getValue()));

}

第五题

第五题

public static void main(String[] args) throws InterruptedException, ExecutionException {

	ExecutorService es = Executors.newFixedThreadPool(3);

	long t1 = System.currentTimeMillis();

	Future<Integer> f1 = es.submit(() -> {
		int sum = 0;
		for (int i = 1; i <= 10000; i++) {
			sum += i;
		}
		return sum;
	});

	Future<Integer> f2 = es.submit(() -> {
		int sum = 0;
		for (int i = 10001; i <= 20000; i++) {
			sum += i;
		}
		return sum;
	});

	Future<Integer> f3 = es.submit(() -> {
		int sum = 0;
		for (int i = 20001; i <= 30000; i++) {
			sum += i;
		}
		return sum;
	});

	int sum = f1.get() + f2.get() + f3.get();

	long t2 = System.currentTimeMillis();

	System.out.println("总和:" + sum);
	System.out.println("耗时:" + (t2 - t1));

	es.shutdown();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值