从数组中选择一个不等于当前值的随机值
Pick a random value from an array that is not equal to the current value
如何在处理过程中将变量的值与数组的内容进行比较?基本上我有一组颜色值。我有一个变量,它是当前颜色。我怎么说:"On this key press: Go through this array of color values, and pick a random value that is NOT equal to the current color. Assign that value as the new current Color?"
color[] colorArray = {#000000,#FFC000,#E0FF00,#7EFF00};
color currentColor;
color randomColor;
void setup(){
size(640,480);
smooth();
noStroke();
currentColor = colorArray[0];
}
void draw(){
background(currentColor);
}
void keyReleased(){
if(key == 's'){
println("currentColor: "+currentColor);
for(int i =0; i < colorArray.length; i++){
//println(colorArray[i]);
if(currentColor != colorArray[i]){
println(colorArray[i]);
// what do i do here? Append to another array and loop through again?
}
}
}
}
也许索引颜色并更改为不同的索引。试试下面的代码,它在 Processing 中运行得很好:
color[] colorArray = {#000000,#FFC000,#E0FF00,#7EFF00};
int currentColor;
void setup(){
size(640,480);
smooth();
noStroke();
currentColor = 0;
}
void draw(){
background(colorArray[currentColor]);
}
void keyReleased(){
if(key == 's'){
println("currentColor: "+currentColor);
int newColor = currentColor;
while (newColor == currentColor)
newColor=(int) random(colorArray.length);
currentColor = newColor;
}
}
我喜欢 James Dunn 的回答,但我想补充一点:这正是 do-while 循环设计的目的。
do-while 至少执行一些代码一次,然后在条件为真时继续执行该代码。在您的情况下,您可以选择一种随机颜色,然后在新颜色与旧颜色相同时不断重复该操作。
这是一个小例子:
color[] colors = {#ff0000, #00ff00, #0000ff};
color currentColor = colors[0];
void draw() {
background(currentColor);
}
void mousePressed() {
color newColor;
do {
newColor = colors[int(random(colors.length))];
} while (newColor == currentColor);
currentColor = newColor;
}
如何在处理过程中将变量的值与数组的内容进行比较?基本上我有一组颜色值。我有一个变量,它是当前颜色。我怎么说:"On this key press: Go through this array of color values, and pick a random value that is NOT equal to the current color. Assign that value as the new current Color?"
color[] colorArray = {#000000,#FFC000,#E0FF00,#7EFF00};
color currentColor;
color randomColor;
void setup(){
size(640,480);
smooth();
noStroke();
currentColor = colorArray[0];
}
void draw(){
background(currentColor);
}
void keyReleased(){
if(key == 's'){
println("currentColor: "+currentColor);
for(int i =0; i < colorArray.length; i++){
//println(colorArray[i]);
if(currentColor != colorArray[i]){
println(colorArray[i]);
// what do i do here? Append to another array and loop through again?
}
}
}
}
也许索引颜色并更改为不同的索引。试试下面的代码,它在 Processing 中运行得很好:
color[] colorArray = {#000000,#FFC000,#E0FF00,#7EFF00};
int currentColor;
void setup(){
size(640,480);
smooth();
noStroke();
currentColor = 0;
}
void draw(){
background(colorArray[currentColor]);
}
void keyReleased(){
if(key == 's'){
println("currentColor: "+currentColor);
int newColor = currentColor;
while (newColor == currentColor)
newColor=(int) random(colorArray.length);
currentColor = newColor;
}
}
我喜欢 James Dunn 的回答,但我想补充一点:这正是 do-while 循环设计的目的。
do-while 至少执行一些代码一次,然后在条件为真时继续执行该代码。在您的情况下,您可以选择一种随机颜色,然后在新颜色与旧颜色相同时不断重复该操作。
这是一个小例子:
color[] colors = {#ff0000, #00ff00, #0000ff};
color currentColor = colors[0];
void draw() {
background(currentColor);
}
void mousePressed() {
color newColor;
do {
newColor = colors[int(random(colors.length))];
} while (newColor == currentColor);
currentColor = newColor;
}