Java 随机字符和随机颜色生成器
我有这两种方法。一个返回字母表中的随机字母,另一个返回随机颜色。 如何编辑这些方法,使Java 随机字符和随机颜色生成器,java,random,colors,Java,Random,Colors,我有这两种方法。一个返回字母表中的随机字母,另一个返回随机颜色。 如何编辑这些方法,使randomLetter()只返回一个字母,而不返回两个相同的字母 如何使我的randomColor()只返回介于红色、黄色、绿色、或蓝色之间的随机颜色?使用枚举: public static char randomLetter() { Random r = new Random(); char randomChar = (char) (97 + r.nextInt(26)); retu
randomLetter()
只返回一个字母,而不返回两个相同的字母
如何使我的
randomColor()
只返回介于红色
、黄色
、绿色
、或蓝色
之间的随机颜色?使用枚举
:
public static char randomLetter() {
Random r = new Random();
char randomChar = (char) (97 + r.nextInt(26));
return randomChar;
}
public static Color randomColor(){
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b);
return randomColor;
}
改用
枚举
:
public static char randomLetter() {
Random r = new Random();
char randomChar = (char) (97 + r.nextInt(26));
return randomChar;
}
public static Color randomColor(){
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b);
return randomColor;
}
随机选择可以包含重复项。如果你不想要复制品,你需要的是洗牌 这里有一种方法可以做到这一点
public enum ColorCode {
RED('r'),
YELLOW('y'),
GREEN('g'),
BLUE('b');
public final char code;
public char getColorCode() {
return this.code;
}
ColorCode(char code) {
this.code = code;
}
}
package com.ggl.testing;
导入java.awt.Color;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类洗牌测试{
私有静态布尔shuffletters=true;
私有静态布尔shuffleColors=true;
私有静态int-shuffletterindex=0;
私有静态int-shuffleColorIndex=0;
私有静态列表字母=new ArrayList();
私有静态列表颜色=新的ArrayList();
公共静态void main(字符串[]args){
System.out.println(shuffletter());
System.out.println(shuffletter());
System.out.println(shuffletter());
System.out.println(shuffleColor());
System.out.println(shuffleColor());
System.out.println(shuffleColor());
}
公共静态字符shuffletter(){
if(shuffletters){
int start=(int)'a';
int end=(int)'z';
对于(int i=start;i一个随机选择可以包含重复项。如果您不想要重复项,那么您需要的是一次洗牌
这里有一种方法可以做到这一点
public enum ColorCode {
RED('r'),
YELLOW('y'),
GREEN('g'),
BLUE('b');
public final char code;
public char getColorCode() {
return this.code;
}
ColorCode(char code) {
this.code = code;
}
}
package com.ggl.testing;
导入java.awt.Color;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类洗牌测试{
私有静态布尔shuffletters=true;
私有静态布尔shuffleColors=true;
私有静态int-shuffletterindex=0;
私有静态int-shuffleColorIndex=0;
私有静态列表字母=new ArrayList();
私有静态列表颜色=新的ArrayList();
公共静态void main(字符串[]args){
System.out.println(shuffletter());
System.out.println(shuffletter());
System.out.println(shuffletter());
System.out.println(shuffleColor());
System.out.println(shuffleColor());
System.out.println(shuffleColor());
}
公共静态字符shuffletter(){
if(shuffletters){
int start=(int)'a';
int end=(int)'z';
对于(int i=开始;i)它是随机的是有原因的,你所说的“介于”是什么意思?如果你想“生成”,红色、黄色、绿色和蓝色是唯一的选项,每次你都要选择1每个元素只有一次,将所有元素放在队列
随机队列中,然后在需要时轮询元素。这是有原因的随机
您所说的“介于之间”是什么意思?红色
、黄色
、绿色
和蓝色
是唯一的选项吗?如果愿意的话只“生成”每个元素一次,将所有元素放入队列洗牌队列中,然后在需要时轮询元素。在代码上,设置洗牌器=false
时你在做什么?我对洗牌一无所知,所以键入集合时你在做什么。洗牌(字母);
?@Kisuna97:shuffletters是第一个时间开关。第一次调用该方法时,它会在返回第一个字母之前创建并洗牌字母。在随后的调用中,该方法会返回列表中的下一个字母。洗牌列表就像洗牌一副牌。您仍然有52张牌,只是顺序不同。我还不明白为什么在if语句中使用shuffleColors
,然后在if
statement@Kisuna97:因为,正如我之前所说,我只需要对列表进行一次洗牌。不是两次,不是三次。一次。在代码上,当设置shuffletters=false时,你在做什么我对shuffle一无所知,所以当你输入集合时你在做什么;
?@Kisuna97:shuffletters是第一个时间开关。第一次调用该方法时,它会在返回第一个字母之前创建并洗牌字母。在随后的调用中,该方法会返回列表中的下一个字母。洗牌列表就像洗牌一副牌。您仍然有52张牌,只是顺序不同。我还不明白为什么在if语句中使用shuffleColors
,然后在if
statement@Kisuna97字体因为,正如我之前所说,我只需要洗牌一次。不是两次,不是三次。一次。