鱼C论坛

 找回密码
 立即注册

地图设计

[复制链接]
发表于 2024-3-14 14:15:46 | 显示全部楼层
卷帘红尘亦爱她 发表于 2024-03-14 14:15
@FishC1用java实现二月是一个有趣的月份,平年的二月 ...


好的,下面是一个满足以上要求的示例程序:

  1. import java.time.LocalDate;
  2. import java.util.Scanner;

  3. public class FebruaryDays {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         
  7.         // 输入年份
  8.         System.out.print("请输入年份:");
  9.         int year = scanner.nextInt();
  10.         
  11.         // 计算二月份天数
  12.         int februaryDays = getFebruaryDays(year);
  13.         System.out.println(year + "年的2月有" + februaryDays + "天。");
  14.         
  15.         // 计算2024年2月1日到今天的天数
  16.         LocalDate startDate = LocalDate.of(2024, 2, 1);
  17.         LocalDate endDate = LocalDate.now();
  18.         int daysBetween = getDaysBetween(startDate, endDate);
  19.         System.out.println("从2024年2月1日到今天,已经过去了" + daysBetween + "天。");
  20.         
  21.         scanner.close();
  22.     }
  23.    
  24.     // 计算二月份天数
  25.     public static int getFebruaryDays(int year) {
  26.         LocalDate date = LocalDate.of(year, 2, 1);
  27.         return date.lengthOfMonth();
  28.     }
  29.    
  30.     // 计算两个日期之间的天数
  31.     public static int getDaysBetween(LocalDate startDate, LocalDate endDate) {
  32.         return Math.abs(startDate.until(endDate).getDays());
  33.     }
  34. }
复制代码


程序中使用了Java 8中提供的日期时间类`LocalDate`来进行日期相关计算。`getFebruaryDays()`方法根据输入的年份获取该年二月份的天数,使用了`lengthOfMonth()`方法。`getDaysBetween()`方法通过`until()`方法计算两个日期之间的天数差。最后,在`main()`方法中调用这两个方法进行计算并输出结果。

希望对你有帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-14 14:19:26 | 显示全部楼层
@FishC1对该代码各行进行注释
  1. import java.util.Scanner;

  2. class UserManagement {
  3.     // 最大登录尝试次数
  4.     private static final int MAX_LOGIN_ATTEMPTS = 3;
  5.     // 密码最小长度
  6.     private static final int PASSWORD_LENGTH_MIN = 8;
  7.     // 密码最大长度
  8.     private static final int PASSWORD_LENGTH_MAX = 12;

  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);

  11.         // 注册
  12.         System.out.print("请输入用户名:");
  13.         String username = scanner.nextLine();
  14.         String password = generateDefaultPassword(username);
  15.         System.out.println("注册成功!您的默认密码是:" + password);

  16.         // 修改密码
  17.         System.out.print("请输入原密码:");
  18.         String oldPassword = scanner.nextLine();
  19.         if (oldPassword.equals(password)) {
  20.             String newPassword;
  21.             do {
  22.                 System.out.print("请输入新密码:");
  23.                 newPassword = scanner.nextLine();
  24.                 System.out.print("请再次输入新密码:");
  25.                 String confirmPassword = scanner.nextLine();

  26.                 if (!validatePassword(newPassword)) {
  27.                     System.out.println("密码必须包含8-12个字符,其中至少包含字母、数字和其他字符中的两种以上,请重新输入!");
  28.                 } else if (!newPassword.equals(confirmPassword)) {
  29.                     System.out.println("两次输入的密码不一致,请重新输入!");
  30.                 } else {
  31.                     password = newPassword;
  32.                     System.out.println("密码修改成功!");
  33.                     break;
  34.                 }
  35.             } while (true);
  36.         } else {
  37.             System.out.println("密码错误,无法修改!");
  38.         }

  39.         // 用户登录
  40.         boolean loggedIn = false;
  41.         for (int i = 1; i <= MAX_LOGIN_ATTEMPTS; i++) {
  42.             System.out.print("请输入用户名:");
  43.             String inputUsername = scanner.nextLine();
  44.             System.out.print("请输入密码:");
  45.             String inputPassword = scanner.nextLine();

  46.             if (inputUsername.equals(username) && inputPassword.equals(password)) {
  47.                 loggedIn = true;
  48.                 break;
  49.             } else {
  50.                 System.out.println("用户名或密码错误,还有" + (MAX_LOGIN_ATTEMPTS - i) + "次尝试机会!");
  51.             }
  52.         }

  53.         if (loggedIn) {
  54.             System.out.println("登录成功!");
  55.         } else {
  56.             System.out.println("登录失败,无法继续登录!");
  57.         }

  58.         scanner.close();
  59.     }

  60.     // 生成默认密码
  61.     private static String generateDefaultPassword(String username) {
  62.         StringBuilder sb = new StringBuilder(username);
  63.         sb.reverse();
  64.         sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
  65.         sb.setCharAt(sb.length()-1, Character.toUpperCase(sb.charAt(sb.length()-1)));

  66.         return sb.toString();
  67.     }

  68.     // 验证密码规则
  69.     private static boolean validatePassword(String password) {
  70.         if (password.length() < PASSWORD_LENGTH_MIN || password.length() > PASSWORD_LENGTH_MAX) {
  71.             return false;
  72.         }

  73.         int categoryCount = 0;
  74.         if (password.matches(".*[a-zA-Z].*")) {
  75.             categoryCount++;
  76.         }
  77.         if (password.matches(".*\\d.*")) {
  78.             categoryCount++;
  79.         }
  80.         if (password.matches(".*[^a-zA-Z0-9].*")) {
  81.             categoryCount++;
  82.         }

  83.         return categoryCount >= 2;
  84.     }
  85. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-14 14:19:57 | 显示全部楼层
卷帘红尘亦爱她 发表于 2024-03-14 14:19
@FishC1对该代码各行进行注释import java.util.Scanne ...


好的,下面是对代码各行进行注释:

  1. import java.util.Scanner;

  2. class UserManagement {
  3.     // 最大登录尝试次数
  4.     private static final int MAX_LOGIN_ATTEMPTS = 3;
  5.     // 密码最小长度
  6.     private static final int PASSWORD_LENGTH_MIN = 8;
  7.     // 密码最大长度
  8.     private static final int PASSWORD_LENGTH_MAX = 12;
  9.    
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         
  13.         // 注册
  14.         System.out.print("请输入用户名:");
  15.         String username = scanner.nextLine();
  16.         String password = generateDefaultPassword(username);
  17.         System.out.println("注册成功!您的默认密码是:" + password);
  18.         
  19.         // 修改密码
  20.         System.out.print("请输入原密码:");
  21.         String oldPassword = scanner.nextLine();
  22.         if (oldPassword.equals(password)) {
  23.             String newPassword;
  24.             do {
  25.                 System.out.print("请输入新密码:");
  26.                 newPassword = scanner.nextLine();
  27.                 System.out.print("请再次输入新密码:");
  28.                 String confirmPassword = scanner.nextLine();
  29.                
  30.                 if (!validatePassword(newPassword)) {
  31.                     System.out.println("密码必须包含8-12个字符,其中至少包含字母、数字和其他字符中的两种以上,请重新输入!");
  32.                 } else if (!newPassword.equals(confirmPassword)) {
  33.                     System.out.println("两次输入的密码不一致,请重新输入!");
  34.                 } else {
  35.                     password = newPassword;
  36.                     System.out.println("密码修改成功!");
  37.                     break;
  38.                 }
  39.             } while (true);
  40.         } else {
  41.             System.out.println("密码错误,无法修改!");
  42.         }
  43.         
  44.         // 用户登录
  45.         boolean loggedIn = false;
  46.         for (int i = 1; i <= MAX_LOGIN_ATTEMPTS; i++) {
  47.             System.out.print("请输入用户名:");
  48.             String inputUsername = scanner.nextLine();
  49.             System.out.print("请输入密码:");
  50.             String inputPassword = scanner.nextLine();
  51.             
  52.             if (inputUsername.equals(username) && inputPassword.equals(password)) {
  53.                 loggedIn = true;
  54.                 break;
  55.             } else {
  56.                 System.out.println("用户名或密码错误。");
  57.             }
  58.         }
  59.         
  60.         if (loggedIn) {
  61.             System.out.println("登录成功!");
  62.         } else {
  63.             System.out.println("超过最大登录尝试次数,登录失败。");
  64.         }
  65.         
  66.         scanner.close();
  67.     }
  68.    
  69.     // 生成默认密码
  70.     public static String generateDefaultPassword(String username) {
  71.         StringBuilder password = new StringBuilder(username);
  72.         password.reverse(); // 反转用户名
  73.         password.setCharAt(0, Character.toUpperCase(password.charAt(0))); // 首字母大写
  74.         password.setCharAt(password.length() - 1, Character.toUpperCase(password.charAt(password.length() - 1))); // 尾字母大写
  75.         return password.toString();
  76.     }
  77.    
  78.     // 验证密码的格式要求
  79.     public static boolean validatePassword(String password) {
  80.         int length = password.length();
  81.         if (length < PASSWORD_LENGTH_MIN || length > PASSWORD_LENGTH_MAX) {
  82.             return false;
  83.         }
  84.         
  85.         int categoryCount = 0;
  86.         if (password.matches(".*[a-zA-Z].*")) { // 包含字母
  87.             categoryCount++;
  88.         }
  89.         if (password.matches(".*\\d.*")) { // 包含数字
  90.             categoryCount++;
  91.         }
  92.         if (password.matches(".*[^a-zA-Z0-9].*")) { // 包含其他字符
  93.             categoryCount++;
  94.         }
  95.         
  96.         return categoryCount >= 2; // 至少包含两种以上类型的字符
  97.     }
  98. }
复制代码

希望这些注释能够帮助你理解代码的功能和实现方式。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-14 14:35:06 | 显示全部楼层
@FishC1运用正则表达式修改该代码
  1. import java.util.Scanner;

  2. class UserManagement {
  3.     // 最大登录尝试次数
  4.     private static final int MAX_LOGIN_ATTEMPTS = 3;
  5.     // 密码最小长度
  6.     private static final int PASSWORD_LENGTH_MIN = 8;
  7.     // 密码最大长度
  8.     private static final int PASSWORD_LENGTH_MAX = 12;
  9.    
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         
  13.         // 注册
  14.         System.out.print("请输入用户名:");
  15.         String username = scanner.nextLine();
  16.         String password = generateDefaultPassword(username);
  17.         System.out.println("注册成功!您的默认密码是:" + password);
  18.         
  19.         // 修改密码
  20.         System.out.print("请输入原密码:");
  21.         String oldPassword = scanner.nextLine();
  22.         if (oldPassword.equals(password)) {
  23.             String newPassword;
  24.             do {
  25.                 System.out.print("请输入新密码:");
  26.                 newPassword = scanner.nextLine();
  27.                 System.out.print("请再次输入新密码:");
  28.                 String confirmPassword = scanner.nextLine();
  29.                
  30.                 if (!validatePassword(newPassword)) {
  31.                     System.out.println("密码必须包含8-12个字符,其中至少包含字母、数字和其他字符中的两种以上,请重新输入!");
  32.                 } else if (!newPassword.equals(confirmPassword)) {
  33.                     System.out.println("两次输入的密码不一致,请重新输入!");
  34.                 } else {
  35.                     password = newPassword;
  36.                     System.out.println("密码修改成功!");
  37.                     break;
  38.                 }
  39.             } while (true);
  40.         } else {
  41.             System.out.println("密码错误,无法修改!");
  42.         }
  43.         
  44.         // 用户登录
  45.         boolean loggedIn = false;
  46.         for (int i = 1; i <= MAX_LOGIN_ATTEMPTS; i++) {
  47.             System.out.print("请输入用户名:");
  48.             String inputUsername = scanner.nextLine();
  49.             System.out.print("请输入密码:");
  50.             String inputPassword = scanner.nextLine();
  51.             
  52.             if (inputUsername.equals(username) && inputPassword.equals(password)) {
  53.                 loggedIn = true;
  54.                 break;
  55.             } else {
  56.                 System.out.println("用户名或密码错误。");
  57.             }
  58.         }
  59.         
  60.         if (loggedIn) {
  61.             System.out.println("登录成功!");
  62.         } else {
  63.             System.out.println("超过最大登录尝试次数,登录失败。");
  64.         }
  65.         
  66.         scanner.close();
  67.     }
  68.    
  69.     // 生成默认密码
  70.     public static String generateDefaultPassword(String username) {
  71.         StringBuilder password = new StringBuilder(username);
  72.         password.reverse(); // 反转用户名
  73.         password.setCharAt(0, Character.toUpperCase(password.charAt(0))); // 首字母大写
  74.         password.setCharAt(password.length() - 1, Character.toUpperCase(password.charAt(password.length() - 1))); // 尾字母大写
  75.         return password.toString();
  76.     }
  77.    
  78.     // 验证密码的格式要求
  79.     public static boolean validatePassword(String password) {
  80.         int length = password.length();
  81.         if (length < PASSWORD_LENGTH_MIN || length > PASSWORD_LENGTH_MAX) {
  82.             return false;
  83.         }
  84.         
  85.         int categoryCount = 0;
  86.         if (password.matches(".*[a-zA-Z].*")) { // 包含字母
  87.             categoryCount++;
  88.         }
  89.         if (password.matches(".*\\d.*")) { // 包含数字
  90.             categoryCount++;
  91.         }
  92.         if (password.matches(".*[^a-zA-Z0-9].*")) { // 包含其他字符
  93.             categoryCount++;
  94.         }
  95.         
  96.         return categoryCount >= 2; // 至少包含两种以上类型的字符
  97.     }
  98. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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(".*[a-zA-Z]+.*", password)) { // 包含字母
            categoryCount++;
        }
        if (Pattern.matches(".*\\d+.*", password)) { // 包含数字
            categoryCount++;
        }
        if (Pattern.matches(".*[^a-zA-Z0-9]+.*", password)) { // 包含其他字符
            categoryCount++;
        }

        return categoryCount >= 2; // 至少包含两种以上类型的字符
    }
}

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-14 15:05:33 | 显示全部楼层
@FishC1根据要求对该代码进行优化1要求编写一个方法,注册。模拟默认密码的自动生成策略,手动输入用户名,根据用户名自动生成默认密码。在生成密码时,将用户名反转并且首尾字母大写即为默认的密码。
2要求编写一个方法,允许用户修改密码,修改密码并再次确认修改后的密码。首先对比两次密码是否一致,给出提示信息,同时密码要求包含8-12个字符,其中至少包含字母、数字和其他字符中的两种以上。密码不符合规则要求反复输入,直到满足规则。
3要求编写一个方法,用户登录。户名和密码输入都正确才会登录成功,否则会提示用户名或密码错误。
要求编写一个方法,模拟用户登录。程序要求如下:
(1)        用户名和密码正确,提示登录成功。
(2)        用户名或密码不正确,提示“用户名或密码错误”。
(3)        总共有3次登录机会,在3次内(包含三次)输入正确的用户名和密码后给出登录成功的相应提示。超过3次用户名或密码输入有误,则提示登录失败,无法再继续登录。
4编写主程序,调用这几个方法,验证程序。
在登录时,需要比较用户输入的用户名密码与已知的用户名密码是否相同,本案例可以使用Scanner类以及String类的相关方法实现比较操作。
  1. import java.util.Scanner;

  2. public class UserManagementSystem {
  3.     private static String username = "admin";
  4.     private static String password = "admin123";

  5.     public static void main(String[] args) {
  6.         register();
  7.         changePassword();
  8.         login();
  9.     }

  10.     public static void register() {
  11.         Scanner scanner = new Scanner(System.in);
  12.         System.out.print("请输入用户名:");
  13.         String userName = scanner.nextLine();
  14.         String generatedPassword = generateDefaultPassword(userName);
  15.         System.out.println("自动生成的密码为:" + generatedPassword);
  16.     }

  17.     public static String generateDefaultPassword(String username) {
  18.         String reversedUsername = new StringBuilder(username).reverse().toString();
  19.         return reversedUsername.substring(0, 1).toUpperCase() + reversedUsername.substring(1, reversedUsername.length() - 1) + reversedUsername.substring(reversedUsername.length() - 1).toUpperCase();
  20.     }

  21.     public static void changePassword() {
  22.         Scanner scanner = new Scanner(System.in);
  23.         while (true) {
  24.             System.out.print("请输入新密码:");
  25.             String newPassword = scanner.nextLine();
  26.             System.out.print("请再次输入新密码:");
  27.             String confirmPassword = scanner.nextLine();
  28.             if (newPassword.equals(confirmPassword) && validatePassword(newPassword)) {
  29.                 System.out.println("密码修改成功!");
  30.                 break;
  31.             } else {
  32.                 System.out.println("密码不符合规则要求,请重新输入!");
  33.             }
  34.         }
  35.     }

  36.     public static boolean validatePassword(String password) {
  37.         return password.length() >= 8 && password.length() <= 12 && !password.matches("[a-zA-Z]+") && !password.matches("\\d+") && !password.matches("[a-zA-Z\\d]+");
  38.     }

  39.     public static void login() {
  40.         Scanner scanner = new Scanner(System.in);
  41.         int tries = 3;
  42.         while (tries > 0) {
  43.             System.out.print("请输入用户名:");
  44.             String inputUsername = scanner.nextLine();
  45.             System.out.print("请输入密码:");
  46.             String inputPassword = scanner.nextLine();
  47.             if (inputUsername.equals(username) && inputPassword.equals(password)) {
  48.                 System.out.println("登录成功!");
  49.                 break;
  50.             } else {
  51.                 System.out.println("用户名或密码错误,还有 " + (tries - 1) + " 次机会");
  52.                 tries--;
  53.             }
  54.         }
  55.         if (tries == 0) {
  56.             System.out.println("登录失败,无法再继续登录!");
  57.         }
  58.     }
  59. }
  60. ```
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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(".*[a-zA-Z].*") &&
               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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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());  // 返回日期间隔天数的绝对值
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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(".*[a-zA-Z]+.*") &&
                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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-14 15:42:03 | 显示全部楼层
@FishC1二月是一个有趣的月份,平年的二月有28天,闰年的二月有29天。本例要求编写一个程序,从键盘输入年份,根据输入的年份计算这一年的2月有多少天。同时计算2024年2月1日到今天有多少天。在计算二月份天数时,要求使用日期时间类的相关方法实现。用java完成该代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-14 15:44:32 | 显示全部楼层
@FishC1用java编写
二月是一个有趣的月份,平年的二月有28天,闰年的二月有29天。本例要求编写一个程序,从键盘输入年份,根据输入的年份计算这一年的2月有多少天。同时计算2024年2月1日到今天有多少天。在计算二月份天数时,要求使用日期时间类的相关方法实现。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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");
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[3578]\\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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-20 14:37:11 | 显示全部楼层
@FishC1 用Java完成该代码大家对百度翻译并不陌生,本案例要求编写一个程序模拟百度翻译。用户输入英文之后搜索程序中对应的中文,如果搜索到对应的中文就输出搜索结果,反之给出提示。本案例要求使用Map集合实现英文与中文的存储(10个以上初始值)。例:word.put("apple", "苹果");
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-20 14:41:50 | 显示全部楼层

@FishC1 用Java完成该代码大家对百度翻译并不陌生,本案例要求编写一个程序模拟百度翻译。用户输入英文之后搜索程序中对应的中文,如果搜索到对应的中文就输出搜索结果,反之给出提示。本案例要求使用Map集合实现英文与中文的存储(10个以上初始值)。例:word.put("apple", "苹果");
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-7 14:05:05 | 显示全部楼层
@FishC1 用Java完成该代码编写一个日记本功能的程序,使用字节流经日记的具体信息记录在本地的txt文件中。当用户输入日记的特定内容后,会将输入的内容保存至本地的txt文件中。需要输入的内容包括“姓名”,“日期”,“天气”、“日记标题”、“内容”的数据。保存的时候需要判断本地是否存在文件,如果存在则追加,不存在则新建。文件命名格式为“XX日记本”加上“.txt”后缀,如“闫雒恒日记本.txt”
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 17:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表