30天学会JAVA—练习题(2021韩顺平)——Day13

这篇博客详细介绍了JAVA学习的第13天内容,涵盖了装拆箱的概念,深入讲解了Sting类、StringBuffer类的使用,以及Arrays类的操作。同时,还提出了关于equals方法比较对象的思考,特别讨论了String、File、Data等特殊类和一般类在值和地址上的区别。

装拆箱

在这里插入图片描述
在这里插入图片描述

Sting类

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

StringBuffer 类

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Arrays类

在这里插入图片描述

public class A04 {
	public static void main(String[] args) {
		//参数数组
		Book[] books = new Book[4];
		books[0] = new Book("红楼梦", 100);
		books[1] = new Book("金瓶梅新", 90);
		books[2] = new Book("青年文摘20年", 5);
		books[3] = new Book("java从入门到放弃~", 300);
		
//		(1)price从小到大
		Arrays.sort(books, new Comparator<Object>() {
			@Override
			public int compare(Object o1, Object o2) {
				Book book1 = (Book) o1;
				Book book2 = (Book) o2;
//				int priceVal =  book1.getPrice() - book2.getPrice(); //price从小到大
//				int priceVal =  book2.getPrice() - book1.getPrice(); //price从大到小
				int priceVal = book1.getName().length() - book2.getName().length();//书名长度从小到大
				//进行转换(如果发现输出的结果不一致,正负数交换就可以了)
				if(priceVal > 0){
					return 1;
				}else if(priceVal < 0){
					return -1;
				}else{
					return 0;
				}
			}
		});
				
		System.out.println(Arrays.toString(books));
	}
}

class Book{
	private String name;
	private int price;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	
	public Book(String name, int price) {
		super();
		this.name = name;
		this.price = price;
	}
	
	@Override
	public String toString() {
		return "Book [name=" + name + ", price=" + price + "]" + "\n";
	}			
}

作业

在这里插入图片描述

public class A05 {
	public static String reverse(String str, int start, int end){	
		//对输入的参数做一个验证
		//重要的编程技巧!!!!
		//(1)写出正确的情况,然后取反即可
		if(!(str != null && start >= 0 && end > start && end < str.length())){
			throw new RuntimeException("参数不正确"); //throw new 运行时异常
		}
				
		char[] chars = str.toCharArray();
		for(int i = start, j = end; i < j; i++,j--){
			char temp = chars[i];
			chars[i] =chars[j];
			chars[j] = temp;
		}
		return new String(chars);
		
	}
	public static void main(String[] args) {
		try {
			System.out.println(A05.reverse("abcdef", 1, 42));
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

在这里插入图片描述

public class A06 {
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String userName = scanner.next();
		System.out.println("请输入密码:");
		String password = scanner.next();
		System.out.println("请输入邮箱:");
		String email = scanner.next();
	
		try {
			userRegister(userName, password, email);
			System.out.println("注册成功!");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		scanner.close();
	}
	
	public static void userRegister(String userName, String password, String email){
		//校验输入是否正确
		if(!(userName.length() >=2 && userName.length() <= 4)){
			throw new RuntimeException("用户名长度不正确(2-4)");
		}
		if(!(password.length() == 6 && isDigital(password))){
			throw new RuntimeException("密码不正确(6位数字)");
		}
		
		int i = email.indexOf('@');
		int j = email.indexOf('.');
		if(!(i > 0 && j < email.length()-1 && i < j)){
			throw new RuntimeException("邮箱不正确,邮箱中包含@和. [@在.前]");
		}
	}
	
	//判断 密码是否全部是数字字符
	public static boolean isDigital(String str){
		// 转换为字符数组
		char[] chars = str.toCharArray();
		for(int i = 0; i < chars.length; i++){
			if(chars[i] < '0' || chars[i] > '9'){
				return false;
			}
		}
		return true;		
	}
}

在这里插入图片描述

public class A07 {
	public static void main(String[] args) {
		System.out.println(printName("Yu Er Lan"));
	}
	
	public static String printName(String str){
		if(str == null){
			System.out.println("姓名不能为空");
		}
		String[] names = str.split(" "); //对字符串进行分割(返回的是数组)
		if(names.length != 3){
			System.out.println("姓名格式不正确: XX XX XX");
		}
		
		String format = String.format("%s,%s .%c", names[2],names[0],names[1].toUpperCase().charAt(0));
		
		return format;		
	}
}

在这里插入图片描述

public class A08 {
	public static void main(String[] args) {
		judge("asAS12@#");
	}
	
	public static void judge(String str){
		if(str == null){
			System.out.println("字符串不能为空");
			return;
		}
		
		int numCount = 0;
		int lowerCount = 0;
		int upperCount = 0;
		int otherCount = 0;
		
		for(int i = 0 ; i < str.length(); i++){
			if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
				numCount++;
			}else if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z'){
				lowerCount++;
			}else if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'){
				upperCount++;
			}else{
				otherCount++;
			}
		}
		System.out.println("数字有:" +numCount + "个");
		System.out.println("小写字母有:" +lowerCount + "个");
		System.out.println("大写字母有:" +upperCount + "个");
		System.out.println("其他字符有:" +otherCount + "个");
	}
}

在这里插入图片描述
在这里插入图片描述

equals比较两个对象

  • 特殊的类:String, File,Data 值
  • 一般类:地址
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值