For loop 是否有其他方法可以更改for循环内的索引?

For loop 是否有其他方法可以更改for循环内的索引?,for-loop,For Loop,假设问题我不想在随机数中取7,所以我这样做。我知道这不是一个好例子。首先很抱歉 int temp[8]; for (int i = 0 ; i < 8 ; i ++ ){ temp[i] = random(8); if (temp[i] == 7){ temp[i] = random(8); i--; } } int-temp[8]; 对于(int i=0;i

假设问题我不想在随机数中取7,所以我这样做。我知道这不是一个好例子。首先很抱歉

int temp[8];

for (int i = 0 ; i < 8 ; i ++ ){
    temp[i] = random(8);
    if (temp[i] == 7){
        temp[i] = random(8);
        i--;
    }
}
int-temp[8];
对于(int i=0;i<8;i++){
温度[i]=随机(8);
如果(温度[i]==7){
温度[i]=随机(8);
我--;
}
}
我的理想是在for循环中不做i。使用额外的空间记录它,然后在第一个for循环外执行somehhing。该守则将成为:

ArrayList i_index;
int count = 0 ;
int temp[8];

for (int i = 0 ; i < 8 ; i ++ ){
    temp[i] = random(8);
    if (temp[i] == 7){
        i_index.add(i) ;    
    }
}

for (int i = 0 ; i < 8 ; i ++ ){
    if ( i_index.get(count) == i ){
        temp[i] ++ ; //do something or whatever just not return 7 
        count ++ ;  
    }
}
arraylisti_索引;
整数计数=0;
内部温度[8];
对于(int i=0;i<8;i++){
温度[i]=随机(8);
如果(温度[i]==7){
i_索引。添加(i);
}
}
对于(int i=0;i<8;i++){
if(i_index.get(count)==i){
temp[i]++;//做点什么或别的什么都不返回7
计数++;
}
}
这是正确的还是有更好的理想?
或者希望有人能给出一个很好的例子。

是的,我通常会使用第二个索引或计数器变量,就像您在代码中显示的那样

count=0;
for(i=0,i<800,i++)
{
    if(x)
    {
       //do something using count
       //ex: array[count] = i+3*count;
       count++;
    }
}
count=0;

对于(i=0,i我想用这种方式尝试,因为我不确定您将使用哪种语言以及XXX和YYY操作的含义

PHP中的示例

$arr=array();

    for($x=0;$x<8;$x++)
    {
        if(xxx)
        {
            array_push($arr,$x);
        }
    }

我相信您正在尝试使用相同的
I
(不在循环体中递减)再次运行循环。一个选项是将增量移动到循环体中,并使用
继续

for (int i = 0 ; i < 8 ; ) { // <-- remove the i++ from here
    if (xxx){
        YYY; // do something
        // i--; // <-- don't need to subtract one from i
        continue; // <-- will rerun the loop with the same i
    }
    i++; // <-- add it here.
}
for (int i = 0 ; i < 8 ; ) { // <-- remove the i++ from here
    if (xxx){
        YYY; // do something
        // i--; // <-- don't need to subtract one from i
    } else {
        i++; // <-- add it here.
    }
}
然后您就不需要
继续;

根据你最近更新的帖子

for (int i = 0 ; i < 8 ; i ++ ){
    temp[i] = random(7); // <-- 0,6 (will never be 7).
}
for(int i=0;i<8;i++){

temp[i]=random(7);//我认为这样操作索引没有任何错误。我经常自己做

例如,最近我不得不在数组中查找可能成对出现但并不总是成对出现的项,因此循环看起来与此类似(它是伪代码):

示例2:或者反过来,在
for
循环中使用
do

for (int i = 0 ; i < 8 ; i ++ )
{
    int num = random(8);
    while(num == 7)
    {
        num = random(8);
    }
    temp[i] = num;
}
for (int i = 0 ; i < 8 ; i ++ )
{
    int num;
    do
    {
        num = random(8);
    } 
    while (num == 7);

    temp[i] = num;
}
示例4:如果您可以使用数组以外的集合,例如列表或可以添加项目的东西,那么它甚至可以更简单:

int num;
bool isValidNumber;
do
{
    num = random(8);
    isValidNumber = (num != 7);
    if (isValidNumber)
    {
        numList.add(num);
    }
}
// Repeat as long as there are fewer then 8 items or the number is invalid
while (numList.count < 8 || !isValidNumber);
int-num;
布尔伊斯瓦利德数;
做
{
num=随机(8);
isValidNumber=(num!=7);
如果(isValidNumber)
{
numList.add(num);
}
}
//只要项目少于8项或编号无效,则重复此操作
while(numList.count<8 | | |!isValidNumber);

语言是什么?我认为javascript不是吗?它不是javascript。看起来可能像C或伪代码。是的,只是伪代码。你到底想实现什么?请参阅编辑。我认为这可能是解决问题的更好方法。是的,你的答案是对的,但使用两个循环。有没有办法只使用一个循环?也许这个示例不好抱歉,我只是想确保循环内的操纵索引是正确的?尽管等等…请参见示例3。似乎是使用另一个循环打击器然后控制循环内的索引。或者只是牺牲额外的空间。如果你可以使用集合而不是数组,它甚至可能看起来像我刚才添加的示例4;-]…但它永远不会是8。哟你不知道这个特殊的随机函数是如何工作的。也许它会生成包含8?;-)的数字OP没有指定
random
的语言或实现,可能他想要8而不是7或任何其他他想要排除的数字。@t3chb0t添加了一种更干净的跳过7的方法。显然,上面的7可以推广到
n
for (int i = 0 ; i < 8 ; i ++ )
{
    int num = random(8);
    while(num == 7)
    {
        num = random(8);
    }
    temp[i] = num;
}
for (int i = 0 ; i < 8 ; i ++ )
{
    int num;
    do
    {
        num = random(8);
    } 
    while (num == 7);

    temp[i] = num;
}
int count = 0;
int num;

do
{
    num = random(8);
    if (num != 7)
    {
        temp[count] = num;
        count++;
    }
}
// Repeat as long as there are fewer then 8 items or the number is 7
while (count < 8 || num == 7);
int num;
bool isValidNumber;
do
{
    num = random(8);
    isValidNumber = (num != 7);
    if (isValidNumber)
    {
        numList.add(num);
    }
}
// Repeat as long as there are fewer then 8 items or the number is invalid
while (numList.count < 8 || !isValidNumber);