一:概述
在生活中,大多数的生日祝福都是通过物质,但作为程序员或者了解学习过程序的人来说。通过程序去实现简单的生日祝福是一件很有意思的事情,在这里将以从简单到稍复杂的程序去实现这个生日祝福的事情。
二:具体实现
<1>通过简单的键盘录入去实现生日祝福
1.1实现代码
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = sc.nextLine();
System.out.println("请输入你的性别:");
char sex = sc.nextLine().charAt(0);
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
sc.nextLine(); // 消费掉换行符
System.out.println("请输入你的出生年月日,格式为:yyyy-MM-dd HH:mm:ss");
String birth = sc.nextLine();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date birthParse = sdf1.parse(birth);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String birthFormat = sdf2.format(birthParse);
System.out.println("您的出生年月日为:" + birthFormat);
System.out.println(name + "你又长大了一岁!祝你" + (age + 1) + "岁生日快乐!祝你开开心心每一天!");
} catch (ParseException e) {
e.printStackTrace();
System.out.println("日期格式不正确,请按照yyyy-MM-dd HH:mm:ss格式输入。");
}
}
1.2案例展示
<2>更复杂的用于向用户发送生日祝福,包括姓名、年龄、祝福语和生日蛋糕图片:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入你的姓名");
String name = input.nextLine();
System.out.println("请输入你的年龄");
int age = Integer.parseInt(input.nextLine());
System.out.println("请输入您的生日");
String birthday = input.nextLine();
String wish = "生日快乐, " + name +"恭喜你," + age +"岁了!";
String wish2 = "愿您在新的一年里,事业有成,健康快乐!";
String imagePath = "birthday_cake.jpg"; // 生日蛋糕图片路径
String regex = "\\d{4}-\\d{2}-\\d{2}"; // 生日日期正则表达式
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(birthday);
if (matcher.find()) {
BufferedImage image = null;
try {
image = ImageIO.read(new File(imagePath));
int width = image.getWidth();
int height = image.getHeight();
//Graphics g = new Graphics(width, height, BufferedImage.TYPE_INT_RGB);
// 注意Graphics是抽象类在Java中,Graphics类是一个抽象类,不能直接实例化。
// 正确的做法是使用BufferedImage创建一个Graphics2D对象来进行绘图操作。
Graphics2D g = image.createGraphics();
g.drawImage(image, 0, 0, null);
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(wish, width / 2 - g.getFontMetrics().stringWidth(wish) / 2, height / 2);
g.drawString(wish2, width / 2 + g.getFontMetrics().stringWidth(wish) / 2, height / 2);
g.dispose();
ImageIO.write(image, "jpg", new File("birthday_card_" + name + ".jpg")); // 保存生日卡片图片到本地
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("请输入正确的生日日期格式(例如:1990-05-17)");
}
这个代码根据实际情况需要改进。
<3>第三方库实现
public class BirthdayCakeGenerator extends JFrame {
private JTextField textField;
private JButton generateButton;
public BirthdayCakeGenerator() {
setTitle("生日祝福和蛋糕生成器");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField(20);
generateButton = new JButton("生成祝福和蛋糕");
generateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String birthday = textField.getText();
LocalDate birthdayDate = LocalDate.parse(birthday);
String birthdayMessage = generateBirthdayMessage(birthdayDate);
saveBirthdayCakeImage(birthdayDate);
JOptionPane.showMessageDialog(null, birthdayMessage + " 生日蛋糕已生成并保存在本地!");
}
});
JPanel panel = new JPanel();
panel.add(new JLabel("请输入生日日期(YYYY-MM-DD): "));
panel.add(textField);
panel.add(generateButton);
add(panel);
setVisible(true);
}
public String generateBirthdayMessage(LocalDate birthdayDate) {
int age = calculateAge(birthdayDate, LocalDate.now());
String message;
if (age < 0) {
message = "生日快乐!祝你永远年轻!";
} else if (age == 0) {
message = "祝宝宝生日快乐!";
} else if (age < 18) {
message = "祝你" + age + "岁生日快乐!健康快乐成长!";
} else if (age < 50) {
message = "祝你" + age + "岁生日快乐!事业蒸蒸日上,家庭幸福美满!";
} else {
message = "祝你" + age + "岁生日快乐!健康长寿,心想事成!";
}
return message;
}
public void saveBirthdayCakeImage(LocalDate birthdayDate) {
try {
File cakeImageFile = new File("birthday_cake.jpg");
// 使用第三方库Thumbnailator生成生日蛋糕图片
Thumbnails.of("cake.jpg")
.size(300, 300)
.outputFormat("jpg")
.toFile(cakeImageFile);
// 将图片保存在本地
String filePath = cakeImageFile.getAbsolutePath();
JOptionPane.showMessageDialog(null, "生日蛋糕图片已保存在本地:" + filePath);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "保存生日蛋糕图片时出错:" + e.getMessage());
}
}
public static int calculateAge(LocalDate birthday, LocalDate today) {
return today.minusYears(birthday.getYear()).getYear();
}
public static void main(String[] args) {
new BirthdayCakeGenerator();
}
}
具体情况还要具体书写。
注意:本篇文章目前只是简单的实现一下。不喜勿喷。