【Java】随机生成生日月份好运榜 switch方法

仅供娱乐!

import java.util.Random;

public class Rad{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Random ra = new Random();
		System.err.println("2022生日好运排名:");
		for (int i = 1; i < 50; i++) {
			int month = ra.nextInt(12)+1;//随机生成月份
			switch(month){
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12: int day = ra.nextInt(31)+1;//随机生成日
			System.out.println(i+"位 "+month+"月"+day+"日");
				break;
			case 2:int day1 = ra.nextInt(29)+1;//随机生成日
			System.out.println(i+"位 "+month+"月"+day1+"日");
				break;
			case 4:
			case 6:
			case 9:
			case 11:int day11 = ra.nextInt(30)+1;//随机生成日
			System.out.println(i+"位 "+month+"月"+day11+"日");
				break;
			}
		}
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Java的Graphics2D类来生成头像。具体步骤如下: 1. 创建一个BufferedImage对象,设置宽高和颜色模式。 2. 获取Graphics2D对象,设置画笔颜色和字体。 3. 绘制背景色或背景图。 4. 绘制头像图案,可以使用随机生成的形状、颜色、大小等。 5. 绘制边框,可以使用随机生成的颜色和大小。 6. 保存头像图片。 以下是一个简单的示例代码: ```java import java.awt.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.util.Random; public class AvatarGenerator { private static final int WIDTH = 200; private static final int HEIGHT = 200; private static final Color[] COLORS = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.ORANGE, Color.PINK}; private static final Random RANDOM = new Random(); public static void main(String[] args) { BufferedImage avatar = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = avatar.createGraphics(); // 设置背景色 g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, WIDTH, HEIGHT); // 绘制头像图案 Shape shape = getRandomShape(); Color color = getRandomColor(); g2d.setColor(color); g2d.fill(shape); // 绘制边框 int borderSize = RANDOM.nextInt(20) + 10; color = getRandomColor(); g2d.setStroke(new BasicStroke(borderSize)); g2d.setColor(color); g2d.draw(shape); // 保存头像图片 try { ImageIO.write(avatar, "png", new File("avatar.png")); } catch (IOException e) { e.printStackTrace(); } } private static Shape getRandomShape() { int shapeType = RANDOM.nextInt(3); switch (shapeType) { case 0: return new Ellipse2D.Double(0, 0, WIDTH, HEIGHT); case 1: return new Rectangle2D.Double(0, 0, WIDTH, HEIGHT); case 2: return new RoundRectangle2D.Double(0, 0, WIDTH, HEIGHT, 50, 50); default: return new Ellipse2D.Double(0, 0, WIDTH, HEIGHT); } } private static Color getRandomColor() { return COLORS[RANDOM.nextInt(COLORS.length)]; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

4444l

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

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

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

打赏作者

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

抵扣说明:

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

余额充值