模拟系统判断是否是你生日java

public class Test5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LocalDate localDate=LocalDate.now();
		LocalDate  birDate=localDate.of(2002, 6, 27); //模拟数据库输入
		MonthDay birmyDay=MonthDay.of(birDate.getMonth(), birDate.getDayOfMonth());
		System.out.println("我的生日是"+birmyDay);
		MonthDay nowMd=MonthDay.from(localDate);
		System.out.println("今天是:"+nowMd);
		boolean flog=birmyDay.equals(nowMd);//比较是否相等
		if(flog) {
			System.out.println("生日快乐去买蛋糕吧");
		}
		else {
			System.out.println("不是你的生日哦别急");
		}		
	}
}

以下是Java实现模拟用户注册的代码,其中使用了HashSet集合来存储用户信息并进行排重: ```java import java.util.HashSet; import java.util.Scanner; public class UserRegister { public static void main(String[] args) { HashSet<User> userSet = new HashSet<>(); // 创建HashSet集合存储用户信息 Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请输入用户名:"); String username = scanner.nextLine(); System.out.println("请输入密码:"); String password = scanner.nextLine(); System.out.println("请再次输入密码:"); String confirmPassword = scanner.nextLine(); System.out.println("请输入生日(格式为yyyy-mm-dd):"); String birthday = scanner.nextLine(); System.out.println("请输入手机号:"); String phone = scanner.nextLine(); System.out.println("请输入邮箱:"); String email = scanner.nextLine(); // 判断输入信息是否正确 if (!isValid(username, password, confirmPassword, birthday, phone, email)) { System.out.println("输入信息有误,请重新输入!"); continue; } // 判断用户是否重复 User user = new User(username, password, birthday, phone, email); if (userSet.contains(user)) { System.out.println("该用户已存在,请重新输入!"); continue; } // 注册成功,将用户信息添加到HashSet集合中 userSet.add(user); System.out.println("注册成功!"); break; } } // 判断输入信息是否正确 public static boolean isValid(String username, String password, String confirmPassword, String birthday, String phone, String email) { if (username.length() < 6 || username.length() > 20) { return false; } if (password.length() < 6 || password.length() > 20) { return false; } if (!password.equals(confirmPassword)) { return false; } if (!birthday.matches("\\d{4}-\\d{2}-\\d{2}")) { return false; } if (!phone.matches("1[3578]\\d{9}")) { return false; } if (!email.contains("@")) { return false; } return true; } } class User { private String username; private String password; private String birthday; private String phone; private String email; public User(String username, String password, String birthday, String phone, String email) { this.username = username; this.password = password; this.birthday = birthday; this.phone = phone; this.email = email; } // 重写equals方法,用于判断用户是否重复 @Override public boolean equals(Object obj) { if (obj instanceof User) { User user = (User) obj; return this.username.equals(user.username); } return false; } // 重写hashCode方法,用于HashSet集合的排重 @Override public int hashCode() { return this.username.hashCode(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小萌新上大分

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值