public class RandomColor {
public String getRandColor() {
String r, g, b;
Random random = new Random();
r = Integer.toHexString(random.nextInt(256)).toUpperCase();
g = Integer.toHexString(random.nextInt(256)).toUpperCase();
b = Integer.toHexString(random.nextInt(256)).toUpperCase();
r = r.length() == 1 ? "0" + r : r;
g = g.length() == 1 ? "0" + g : g;
b = b.length() == 1 ? "0" + b : b;
// return "#" + r + g + b;
return r+g+b;
}
}
本文介绍了一个简单的Java程序,用于生成随机颜色代码。该程序通过使用Random类生成RGB值,并将其转换为十六进制形式,方便在网页设计或其他需要颜色代码的应用中使用。
2925

被折叠的 条评论
为什么被折叠?



