FishC
发表于 2024-3-14 14:15:46
卷帘红尘亦爱她 发表于 2024-03-14 14:15
@FishC1用java实现二月是一个有趣的月份,平年的二月 ...
好的,下面是一个满足以上要求的示例程序:
import java.time.LocalDate;
import java.util.Scanner;
public class FebruaryDays {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入年份
System.out.print("请输入年份:");
int year = scanner.nextInt();
// 计算二月份天数
int februaryDays = getFebruaryDays(year);
System.out.println(year + "年的2月有" + februaryDays + "天。");
// 计算2024年2月1日到今天的天数
LocalDate startDate = LocalDate.of(2024, 2, 1);
LocalDate endDate = LocalDate.now();
int daysBetween = getDaysBetween(startDate, endDate);
System.out.println("从2024年2月1日到今天,已经过去了" + daysBetween + "天。");
scanner.close();
}
// 计算二月份天数
public static int getFebruaryDays(int year) {
LocalDate date = LocalDate.of(year, 2, 1);
return date.lengthOfMonth();
}
// 计算两个日期之间的天数
public static int getDaysBetween(LocalDate startDate, LocalDate endDate) {
return Math.abs(startDate.until(endDate).getDays());
}
}
程序中使用了Java 8中提供的日期时间类`LocalDate`来进行日期相关计算。`getFebruaryDays()`方法根据输入的年份获取该年二月份的天数,使用了`lengthOfMonth()`方法。`getDaysBetween()`方法通过`until()`方法计算两个日期之间的天数差。最后,在`main()`方法中调用这两个方法进行计算并输出结果。
希望对你有帮助!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-3-14 14:19:26
@FishC1对该代码各行进行注释import java.util.Scanner;
class UserManagement {
// 最大登录尝试次数
private static final int MAX_LOGIN_ATTEMPTS = 3;
// 密码最小长度
private static final int PASSWORD_LENGTH_MIN = 8;
// 密码最大长度
private static final int PASSWORD_LENGTH_MAX = 12;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 注册
System.out.print("请输入用户名:");
String username = scanner.nextLine();
String password = generateDefaultPassword(username);
System.out.println("注册成功!您的默认密码是:" + password);
// 修改密码
System.out.print("请输入原密码:");
String oldPassword = scanner.nextLine();
if (oldPassword.equals(password)) {
String newPassword;
do {
System.out.print("请输入新密码:");
newPassword = scanner.nextLine();
System.out.print("请再次输入新密码:");
String confirmPassword = scanner.nextLine();
if (!validatePassword(newPassword)) {
System.out.println("密码必须包含8-12个字符,其中至少包含字母、数字和其他字符中的两种以上,请重新输入!");
} else if (!newPassword.equals(confirmPassword)) {
System.out.println("两次输入的密码不一致,请重新输入!");
} else {
password = newPassword;
System.out.println("密码修改成功!");
break;
}
} while (true);
} else {
System.out.println("密码错误,无法修改!");
}
// 用户登录
boolean loggedIn = false;
for (int i = 1; i <= MAX_LOGIN_ATTEMPTS; i++) {
System.out.print("请输入用户名:");
String inputUsername = scanner.nextLine();
System.out.print("请输入密码:");
String inputPassword = scanner.nextLine();
if (inputUsername.equals(username) && inputPassword.equals(password)) {
loggedIn = true;
break;
} else {
System.out.println("用户名或密码错误,还有" + (MAX_LOGIN_ATTEMPTS - i) + "次尝试机会!");
}
}
if (loggedIn) {
System.out.println("登录成功!");
} else {
System.out.println("登录失败,无法继续登录!");
}
scanner.close();
}
// 生成默认密码
private static String generateDefaultPassword(String username) {
StringBuilder sb = new StringBuilder(username);
sb.reverse();
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
sb.setCharAt(sb.length()-1, Character.toUpperCase(sb.charAt(sb.length()-1)));
return sb.toString();
}
// 验证密码规则
private static boolean validatePassword(String password) {
if (password.length() < PASSWORD_LENGTH_MIN || password.length() > PASSWORD_LENGTH_MAX) {
return false;
}
int categoryCount = 0;
if (password.matches(".*.*")) {
categoryCount++;
}
if (password.matches(".*\\d.*")) {
categoryCount++;
}
if (password.matches(".*[^a-zA-Z0-9].*")) {
categoryCount++;
}
return categoryCount >= 2;
}
}
FishC
发表于 2024-3-14 14:19:57
卷帘红尘亦爱她 发表于 2024-03-14 14:19
@FishC1对该代码各行进行注释import java.util.Scanne ...
好的,下面是对代码各行进行注释:
import java.util.Scanner;
class UserManagement {
// 最大登录尝试次数
private static final int MAX_LOGIN_ATTEMPTS = 3;
// 密码最小长度
private static final int PASSWORD_LENGTH_MIN = 8;
// 密码最大长度
private static final int PASSWORD_LENGTH_MAX = 12;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 注册
System.out.print("请输入用户名:");
String username = scanner.nextLine();
String password = generateDefaultPassword(username);
System.out.println("注册成功!您的默认密码是:" + password);
// 修改密码
System.out.print("请输入原密码:");
String oldPassword = scanner.nextLine();
if (oldPassword.equals(password)) {
String newPassword;
do {
System.out.print("请输入新密码:");
newPassword = scanner.nextLine();
System.out.print("请再次输入新密码:");
String confirmPassword = scanner.nextLine();
if (!validatePassword(newPassword)) {
System.out.println("密码必须包含8-12个字符,其中至少包含字母、数字和其他字符中的两种以上,请重新输入!");
} else if (!newPassword.equals(confirmPassword)) {
System.out.println("两次输入的密码不一致,请重新输入!");
} else {
password = newPassword;
System.out.println("密码修改成功!");
break;
}
} while (true);
} else {
System.out.println("密码错误,无法修改!");
}
// 用户登录
boolean loggedIn = false;
for (int i = 1; i <= MAX_LOGIN_ATTEMPTS; i++) {
System.out.print("请输入用户名:");
String inputUsername = scanner.nextLine();
System.out.print("请输入密码:");
String inputPassword = scanner.nextLine();
if (inputUsername.equals(username) && inputPassword.equals(password)) {
loggedIn = true;
break;
} else {
System.out.println("用户名或密码错误。");
}
}
if (loggedIn) {
System.out.println("登录成功!");
} else {
System.out.println("超过最大登录尝试次数,登录失败。");
}
scanner.close();
}
// 生成默认密码
public static String generateDefaultPassword(String username) {
StringBuilder password = new StringBuilder(username);
password.reverse(); // 反转用户名
password.setCharAt(0, Character.toUpperCase(password.charAt(0))); // 首字母大写
password.setCharAt(password.length() - 1, Character.toUpperCase(password.charAt(password.length() - 1))); // 尾字母大写
return password.toString();
}
// 验证密码的格式要求
public static boolean validatePassword(String password) {
int length = password.length();
if (length < PASSWORD_LENGTH_MIN || length > PASSWORD_LENGTH_MAX) {
return false;
}
int categoryCount = 0;
if (password.matches(".*.*")) { // 包含字母
categoryCount++;
}
if (password.matches(".*\\d.*")) { // 包含数字
categoryCount++;
}
if (password.matches(".*[^a-zA-Z0-9].*")) { // 包含其他字符
categoryCount++;
}
return categoryCount >= 2; // 至少包含两种以上类型的字符
}
}
希望这些注释能够帮助你理解代码的功能和实现方式。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-3-14 14:35:06
@FishC1运用正则表达式修改该代码import java.util.Scanner;
class UserManagement {
// 最大登录尝试次数
private static final int MAX_LOGIN_ATTEMPTS = 3;
// 密码最小长度
private static final int PASSWORD_LENGTH_MIN = 8;
// 密码最大长度
private static final int PASSWORD_LENGTH_MAX = 12;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 注册
System.out.print("请输入用户名:");
String username = scanner.nextLine();
String password = generateDefaultPassword(username);
System.out.println("注册成功!您的默认密码是:" + password);
// 修改密码
System.out.print("请输入原密码:");
String oldPassword = scanner.nextLine();
if (oldPassword.equals(password)) {
String newPassword;
do {
System.out.print("请输入新密码:");
newPassword = scanner.nextLine();
System.out.print("请再次输入新密码:");
String confirmPassword = scanner.nextLine();
if (!validatePassword(newPassword)) {
System.out.println("密码必须包含8-12个字符,其中至少包含字母、数字和其他字符中的两种以上,请重新输入!");
} else if (!newPassword.equals(confirmPassword)) {
System.out.println("两次输入的密码不一致,请重新输入!");
} else {
password = newPassword;
System.out.println("密码修改成功!");
break;
}
} while (true);
} else {
System.out.println("密码错误,无法修改!");
}
// 用户登录
boolean loggedIn = false;
for (int i = 1; i <= MAX_LOGIN_ATTEMPTS; i++) {
System.out.print("请输入用户名:");
String inputUsername = scanner.nextLine();
System.out.print("请输入密码:");
String inputPassword = scanner.nextLine();
if (inputUsername.equals(username) && inputPassword.equals(password)) {
loggedIn = true;
break;
} else {
System.out.println("用户名或密码错误。");
}
}
if (loggedIn) {
System.out.println("登录成功!");
} else {
System.out.println("超过最大登录尝试次数,登录失败。");
}
scanner.close();
}
// 生成默认密码
public static String generateDefaultPassword(String username) {
StringBuilder password = new StringBuilder(username);
password.reverse(); // 反转用户名
password.setCharAt(0, Character.toUpperCase(password.charAt(0))); // 首字母大写
password.setCharAt(password.length() - 1, Character.toUpperCase(password.charAt(password.length() - 1))); // 尾字母大写
return password.toString();
}
// 验证密码的格式要求
public static boolean validatePassword(String password) {
int length = password.length();
if (length < PASSWORD_LENGTH_MIN || length > PASSWORD_LENGTH_MAX) {
return false;
}
int categoryCount = 0;
if (password.matches(".*.*")) { // 包含字母
categoryCount++;
}
if (password.matches(".*\\d.*")) { // 包含数字
categoryCount++;
}
if (password.matches(".*[^a-zA-Z0-9].*")) { // 包含其他字符
categoryCount++;
}
return categoryCount >= 2; // 至少包含两种以上类型的字符
}
}
FishC
发表于 2024-3-14 14:35:27
卷帘红尘亦爱她 发表于 2024-03-14 14:35
@FishC1运用正则表达式修改该代码import java.util.Sc ...
import java.util.Scanner;
import java.util.regex.Pattern;
class UserManagement {
private static final int MAX_LOGIN_ATTEMPTS = 3;
private static final int PASSWORD_LENGTH_MIN = 8;
private static final int PASSWORD_LENGTH_MAX = 12;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 注册
System.out.print("请输入用户名:");
String username = scanner.nextLine();
String password = generateDefaultPassword(username);
System.out.println("注册成功!您的默认密码是:" + password);
// 修改密码
System.out.print("请输入原密码:");
String oldPassword = scanner.nextLine();
if (oldPassword.equals(password)) {
String newPassword;
do {
System.out.print("请输入新密码:");
newPassword = scanner.nextLine();
System.out.print("请再次输入新密码:");
String confirmPassword = scanner.nextLine();
if (!validatePassword(newPassword)) {
System.out.println("密码必须包含8-12个字符,其中至少包含字母、数字和其他字符中的两种以上,请重新输入!");
} else if (!newPassword.equals(confirmPassword)) {
System.out.println("两次输入的密码不一致,请重新输入!");
} else {
password = newPassword;
System.out.println("密码修改成功!");
break;
}
} while (true);
} else {
System.out.println("密码错误,无法修改!");
}
// 用户登录
boolean loggedIn = false;
for (int i = 1; i <= MAX_LOGIN_ATTEMPTS; i++) {
System.out.print("请输入用户名:");
String inputUsername = scanner.nextLine();
System.out.print("请输入密码:");
String inputPassword = scanner.nextLine();
if (inputUsername.equals(username) && inputPassword.equals(password)) {
loggedIn = true;
System.out.println("登录成功!");
break;
} else {
System.out.println("登录失败,您还有" + (MAX_LOGIN_ATTEMPTS - i) + "次尝试机会!");
}
}
if (!loggedIn) {
System.out.println("登录尝试次数超过上限,账号已锁定!");
}
scanner.close();
}
private static String generateDefaultPassword(String username) {
return "defaultPassword";
}
private static boolean validatePassword(String password) {
if (password.length() < PASSWORD_LENGTH_MIN || password.length() > PASSWORD_LENGTH_MAX) {
return false;
}
int categoryCount = 0;
if (Pattern.matches(".*+.*", password)) { // 包含字母
categoryCount++;
}
if (Pattern.matches(".*\\d+.*", password)) { // 包含数字
categoryCount++;
}
if (Pattern.matches(".*[^a-zA-Z0-9]+.*", password)) { // 包含其他字符
categoryCount++;
}
return categoryCount >= 2; // 至少包含两种以上类型的字符
}
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-3-14 15:05:33
@FishC1根据要求对该代码进行优化1要求编写一个方法,注册。模拟默认密码的自动生成策略,手动输入用户名,根据用户名自动生成默认密码。在生成密码时,将用户名反转并且首尾字母大写即为默认的密码。
2要求编写一个方法,允许用户修改密码,修改密码并再次确认修改后的密码。首先对比两次密码是否一致,给出提示信息,同时密码要求包含8-12个字符,其中至少包含字母、数字和其他字符中的两种以上。密码不符合规则要求反复输入,直到满足规则。
3要求编写一个方法,用户登录。户名和密码输入都正确才会登录成功,否则会提示用户名或密码错误。
要求编写一个方法,模拟用户登录。程序要求如下:
(1) 用户名和密码正确,提示登录成功。
(2) 用户名或密码不正确,提示“用户名或密码错误”。
(3) 总共有3次登录机会,在3次内(包含三次)输入正确的用户名和密码后给出登录成功的相应提示。超过3次用户名或密码输入有误,则提示登录失败,无法再继续登录。
4编写主程序,调用这几个方法,验证程序。
在登录时,需要比较用户输入的用户名密码与已知的用户名密码是否相同,本案例可以使用Scanner类以及String类的相关方法实现比较操作。
import java.util.Scanner;
public class UserManagementSystem {
private static String username = "admin";
private static String password = "admin123";
public static void main(String[] args) {
register();
changePassword();
login();
}
public static void register() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String userName = scanner.nextLine();
String generatedPassword = generateDefaultPassword(userName);
System.out.println("自动生成的密码为:" + generatedPassword);
}
public static String generateDefaultPassword(String username) {
String reversedUsername = new StringBuilder(username).reverse().toString();
return reversedUsername.substring(0, 1).toUpperCase() + reversedUsername.substring(1, reversedUsername.length() - 1) + reversedUsername.substring(reversedUsername.length() - 1).toUpperCase();
}
public static void changePassword() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入新密码:");
String newPassword = scanner.nextLine();
System.out.print("请再次输入新密码:");
String confirmPassword = scanner.nextLine();
if (newPassword.equals(confirmPassword) && validatePassword(newPassword)) {
System.out.println("密码修改成功!");
break;
} else {
System.out.println("密码不符合规则要求,请重新输入!");
}
}
}
public static boolean validatePassword(String password) {
return password.length() >= 8 && password.length() <= 12 && !password.matches("+") && !password.matches("\\d+") && !password.matches("+");
}
public static void login() {
Scanner scanner = new Scanner(System.in);
int tries = 3;
while (tries > 0) {
System.out.print("请输入用户名:");
String inputUsername = scanner.nextLine();
System.out.print("请输入密码:");
String inputPassword = scanner.nextLine();
if (inputUsername.equals(username) && inputPassword.equals(password)) {
System.out.println("登录成功!");
break;
} else {
System.out.println("用户名或密码错误,还有 " + (tries - 1) + " 次机会");
tries--;
}
}
if (tries == 0) {
System.out.println("登录失败,无法再继续登录!");
}
}
}
```
FishC
发表于 2024-3-14 15:05:49
卷帘红尘亦爱她 发表于 2024-03-14 15:05
@FishC1根据要求对该代码进行优化1要求编写一个方法, ...
import java.util.Scanner;
public class UserManagementSystem {
private static String username = "admin";
private static String password = "admin123";
private static int loginAttempts = 3;
public static void main(String[] args) {
register();
changePassword();
login();
}
public static void register() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String userName = scanner.nextLine();
String generatedPassword = generateDefaultPassword(userName);
System.out.println("自动生成的密码为:" + generatedPassword);
}
public static String generateDefaultPassword(String username) {
String reversedUsername = new StringBuilder(username).reverse().toString();
return reversedUsername.substring(0, 1).toUpperCase() +
reversedUsername.substring(1, reversedUsername.length() - 1) +
reversedUsername.substring(reversedUsername.length() - 1).toUpperCase();
}
public static void changePassword() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入新密码:");
String newPassword = scanner.nextLine();
System.out.print("请再次输入新密码:");
String confirmPassword = scanner.nextLine();
if (newPassword.equals(confirmPassword) && validatePassword(newPassword)) {
System.out.println("密码修改成功!");
break;
} else {
System.out.println("密码不符合规则要求,请重新输入!");
}
}
}
public static boolean validatePassword(String password) {
return password.length() >= 8 &&
password.length() <= 12 &&
password.matches(".*.*") &&
password.matches(".*\\d.*") &&
password.matches(".*[^a-zA-Z\\d].*");
}
public static void login() {
Scanner scanner = new Scanner(System.in);
int tries = loginAttempts;
while (tries > 0) {
System.out.print("请输入用户名:");
String inputUsername = scanner.nextLine();
System.out.print("请输入密码:");
String inputPassword = scanner.nextLine();
if (inputUsername.equals(username) && inputPassword.equals(password)) {
System.out.println("登录成功!");
break;
} else {
System.out.println("用户名或密码错误,还有 " + (tries - 1) + " 次机会");
tries--;
}
}
if (tries == 0) {
System.out.println("登录失败,无法再继续登录!");
}
}
}
```
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-3-14 15:30:42
@FishC1对该代码进行修改import java.time.LocalDate;// 导入 LocalDate 类
import java.util.Scanner;// 导入 Scanner 类
class FebruaryDays {// 定义类 FebruaryDays
public static void main(String[] args) {// 主方法
Scanner scanner = new Scanner(System.in);// 创建 Scanner 对象
// 输入年份
System.out.print("请输入年份:");
int year = scanner.nextInt();// 从控制台读取输入的年份
// 计算二月份天数
int eryuedays = eryuetian(year);// 调用方法计算二月份天数
System.out.println(year + "年的2月有" + eryuedays + "天。");// 打印二月份天数信息
// 计算2024年2月1日到今天的天数
LocalDate startDate = LocalDate.of(2024, 2, 1);// 创建开始日期对象
LocalDate endDate = LocalDate.now();// 创建当前日期对象
int xiangcha = getDaysBetween(startDate, endDate);// 调用方法计算两个日期之间的天数
System.out.println("从2024年2月1日到今天,已经过去了" + xiangcha + "天。");// 打印日期间隔信息
scanner.close();// 关闭 Scanner 对象
}
// 计算二月份天数
public static int eryuetian(int year) {// 计算二月份天数方法
LocalDate date = LocalDate.of(year, 2, 1);// 创建日期对象
return date.lengthOfMonth();// 返回二月份天数
}
// 计算两个日期之间的天数
public static int getDaysBetween(LocalDate startDate, LocalDate endDate) {// 计算日期间隔方法
return Math.abs(startDate.until(endDate).getDays());// 返回日期间隔天数的绝对值
}
}
FishC
发表于 2024-3-14 15:31:09
卷帘红尘亦爱她 发表于 2024-03-14 15:30
@FishC1对该代码进行修改
import java.util.Scanner;
public class UserManagementSystem {
private static String username = "admin";
private static String password = "admin123";
private static int maxLoginAttempts = 3;
public static void main(String[] args) {
register();
changePassword();
login();
}
public static void register() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String userName = scanner.nextLine();
String generatedPassword = generateDefaultPassword(userName);
System.out.println("自动生成的密码为:" + generatedPassword);
}
public static String generateDefaultPassword(String username) {
StringBuilder reversedUsername = new StringBuilder(username).reverse();
String defaultPassword = reversedUsername.toString().toUpperCase();
return defaultPassword.substring(0, 1) + reversedUsername.substring(1);
}
public static void changePassword() {
Scanner scanner = new Scanner(System.in);
String newPassword;
do {
System.out.print("请输入新密码:");
newPassword = scanner.nextLine();
System.out.print("请再次输入新密码:");
String confirmPassword = scanner.nextLine();
if (validatePassword(newPassword) && newPassword.equals(confirmPassword)) {
System.out.println("密码修改成功!");
break;
} else {
System.out.println("密码不符合规则要求,请重新输入!");
}
} while (true);
}
public static boolean validatePassword(String password) {
return password.length() >= 8 && password.length() <= 12 &&
password.matches(".*+.*") &&
password.matches(".*\\d+.*") &&
password.matches(".*[^a-zA-Z\\d]+.*");
}
public static void login() {
Scanner scanner = new Scanner(System.in);
int loginAttempts = 0;
while (loginAttempts < maxLoginAttempts) {
System.out.print("请输入用户名:");
String inputUsername = scanner.nextLine();
System.out.print("请输入密码:");
String inputPassword = scanner.nextLine();
if (inputUsername.equals(username) && inputPassword.equals(password)) {
System.out.println("登录成功!");
break;
} else {
System.out.println("用户名或密码错误,还有 " + (maxLoginAttempts - loginAttempts - 1) + " 次机会");
loginAttempts++;
}
}
if (loginAttempts == maxLoginAttempts) {
System.out.println("登录失败,无法再继续登录!");
}
}
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-3-14 15:42:03
@FishC1二月是一个有趣的月份,平年的二月有28天,闰年的二月有29天。本例要求编写一个程序,从键盘输入年份,根据输入的年份计算这一年的2月有多少天。同时计算2024年2月1日到今天有多少天。在计算二月份天数时,要求使用日期时间类的相关方法实现。用java完成该代码
FishC
发表于 2024-3-14 15:42:17
卷帘红尘亦爱她 发表于 2024-03-14 15:42
@FishC1二月是一个有趣的月份,平年的二月有28天,闰 ...
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class FebruaryDaysCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
int daysInFebruary = getDaysInFebruary(year);
System.out.println(year + "年的2月份有" + daysInFebruary + "天。");
LocalDate today = LocalDate.now();
LocalDate februaryFirst2024 = LocalDate.of(2024, 2, 1);
long daysSince2024 = ChronoUnit.DAYS.between(februaryFirst2024, today);
System.out.println("从2024年2月1日到今天一共过去了" + daysSince2024 + "天。");
}
public static int getDaysInFebruary(int year) {
if (LocalDate.ofYearDay(year, 1).isLeapYear()) {
return 29;
} else {
return 28;
}
}
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-3-14 15:44:32
@FishC1用java编写
二月是一个有趣的月份,平年的二月有28天,闰年的二月有29天。本例要求编写一个程序,从键盘输入年份,根据输入的年份计算这一年的2月有多少天。同时计算2024年2月1日到今天有多少天。在计算二月份天数时,要求使用日期时间类的相关方法实现。
FishC
发表于 2024-3-14 15:44:48
卷帘红尘亦爱她 发表于 2024-03-14 15:44
@FishC1用java编写
二月是一个有趣的月份,平年的二 ...
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class FebruaryDays {
public static void main(String[] args) {
// 获取当前日期
LocalDate today = LocalDate.now();
// 获取输入的年份
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
// 判断是否为闰年
boolean isLeapYear = LocalDate.ofYearDay(year, 1).isLeapYear();
// 计算二月份天数
int februaryDays = isLeapYear ? 29 : 28;
System.out.println(year + "年的二月有" + februaryDays + "天。");
// 计算2024年2月1日到今天的天数
LocalDate startDate = LocalDate.of(2024, 2, 1);
long daysBetween = ChronoUnit.DAYS.between(startDate, today);
System.out.println("从2024年2月1日到今天共有" + daysBetween + "天。");
}
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-3-20 14:03:52
@FishC1 用java实现下列功能本例要求编写一个程序,模拟用户注册。用户输入用户名、密码、确认密码、生日(格式为yyyy--mm—dd为正确)、手机号(手机号长度为11位,并且以13、15、17、18为开头的手机号为正确)、邮箱(包含符号“@”为正确)信息之后,判断信息正确后,验证用户是否重复,重复则给出相应提示,如果不重复则注册成功。案例要求使用HashSet集合实现(需初始注册用户3个以上),创建用户类,并重写其HashCode()和equals()方法。
User user = new User("yanluoheng", "ylh123", new Date(),"15003895606", "yanluoheng@ncwu.edu.cn");
FishC
发表于 2024-3-20 14:04:13
卷帘红尘亦爱她 发表于 2024-03-20 14:03
@FishC1 用java实现下列功能本例要求编写一个程序,模 ...
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class User {
private String username;
private String password;
private Date birthday;
private String phoneNumber;
private String email;
public User(String username, String password, Date birthday, String phoneNumber, String email) {
this.username = username;
this.password = password;
this.birthday = birthday;
this.phoneNumber = phoneNumber;
this.email = email;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((username == null) ? 0 : username.hashCode());
result = prime * result + ((email == null) ? 0 : email.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;
User other = (User) obj;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
return true;
}
}
public class UserRegistration {
public static void main(String[] args) {
Set<User> users = new HashSet<>();
users.add(new User("yanluoheng", "ylh123", new Date(), "15003895606", "yanluoheng@ncwu.edu.cn"));
users.add(new User("xiaoming", "xm456", new Date(), "13800567890", "xiaoming@example.com"));
users.add(new User("jingjing", "jj789", new Date(), "13500234567", "jingjing@example.com"));
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String username = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
System.out.print("请确认密码:");
String confirmPassword = scanner.nextLine();
System.out.print("请输入生日(yyyy-mm-dd):");
String birthdayString = scanner.nextLine();
Date birthday = null;
try {
birthday = new SimpleDateFormat("yyyy-MM-dd").parse(birthdayString);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.print("请输入手机号码:");
String phoneNumber = scanner.nextLine();
System.out.print("请输入邮箱:");
String email = scanner.nextLine();
// 判断信息是否正确
if (!password.equals(confirmPassword)) {
System.out.println("密码和确认密码不一致!");
} else if (!phoneNumber.matches("^1\\d{9}$")) {
System.out.println("手机号格式不正确!");
} else if (!email.contains("@")) {
System.out.println("邮箱格式不正确!");
} else {
User newUser = new User(username, password, birthday, phoneNumber, email);
// 判断用户是否已存在
if (users.contains(newUser)) {
System.out.println("用户已存在!");
} else {
users.add(newUser);
System.out.println("注册成功!");
}
}
scanner.close();
}
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-3-20 14:37:11
@FishC1 用Java完成该代码大家对百度翻译并不陌生,本案例要求编写一个程序模拟百度翻译。用户输入英文之后搜索程序中对应的中文,如果搜索到对应的中文就输出搜索结果,反之给出提示。本案例要求使用Map集合实现英文与中文的存储(10个以上初始值)。例:word.put("apple", "苹果");
FishC
发表于 2024-3-20 14:37:39
卷帘红尘亦爱她 发表于 2024-03-20 14:37
@FishC1 用Java完成该代码大家对百度翻译并不陌生,本 ...
import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class BaiduTranslate { public static void main(String[] args) { Map<String, String> wordMap = new HashMap<>(); // 初始化单词映射 wordMap.put("apple", "苹果"); wordMap.put("banana", "香蕉"); wordMap.put("cat", "猫"); wordMap.put("dog", "狗"); wordMap.put("elephant", "大象"); wordMap.put("flower", "花"); wordMap.put("girl", "女孩"); wordMap.put("hello", "你好"); wordMap.put("ice cream", "冰淇淋"); wordMap.put("juice", "果汁"); Scanner scanner = new Scanner(System.in); System.out.print("请输入英文单词:"); String word = scanner.nextLine(); if (wordMap.containsKey(word)) { System.out.println("中文: " + wordMap.get(word)); } else { System.out.println("未找到对应的中文翻译"); } }}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-3-20 14:41:50
@FishC1 用Java完成该代码大家对百度翻译并不陌生,本案例要求编写一个程序模拟百度翻译。用户输入英文之后搜索程序中对应的中文,如果搜索到对应的中文就输出搜索结果,反之给出提示。本案例要求使用Map集合实现英文与中文的存储(10个以上初始值)。例:word.put("apple", "苹果");
FishC
发表于 2024-3-20 14:42:04
卷帘红尘亦爱她 发表于 2024-03-20 14:41
@FishC1 用Java完成该代码大家对百度翻译并不陌生,本 ...
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class BaiduTranslate {
public static void main(String[] args) {
// 创建一个Map集合,用于存储英文和中文的对应关系
Map<String, String> wordMap = new HashMap<>();
// 添加初始值
wordMap.put("apple", "苹果");
wordMap.put("banana", "香蕉");
wordMap.put("cat", "猫");
wordMap.put("dog", "狗");
wordMap.put("elephant", "大象");
wordMap.put("flower", "花");
wordMap.put("guitar", "吉他");
wordMap.put("house", "房子");
wordMap.put("ice cream", "冰淇淋");
wordMap.put("jam", "果酱");
// 创建一个Scanner对象,用于接收用户输入
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要翻译的英文单词或短语:");
String input = scanner.nextLine();
// 搜索Map中对应的中文
String result = wordMap.get(input);
// 输出搜索结果或提示信息
if (result != null) {
System.out.println("搜索结果:" + result);
} else {
System.out.println("未找到对应的中文翻译!");
}
}
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
卷帘红尘亦爱她
发表于 2024-4-7 14:05:05
@FishC1 用Java完成该代码编写一个日记本功能的程序,使用字节流经日记的具体信息记录在本地的txt文件中。当用户输入日记的特定内容后,会将输入的内容保存至本地的txt文件中。需要输入的内容包括“姓名”,“日期”,“天气”、“日记标题”、“内容”的数据。保存的时候需要判断本地是否存在文件,如果存在则追加,不存在则新建。文件命名格式为“XX日记本”加上“.txt”后缀,如“闫雒恒日记本.txt”
页:
1
2
3
4
5
[6]
7
8
9
10