2016-11-13 163 views
-1

我有這個c代碼,關於多個if語句.. 所有語句都很好地工作,除了最後一個.. 它被其他if語句激活並因此破壞結果.. 任何建議?其他多個if語句

for (i=0; i<(total); i++) 
    { 
    for (n=0; n<((total)/4); n++) 
    { 
     if (i==(0+(n*4)) && i<(total-5)&&(array[i]==0)) 
     { 
      array[i]=1; 
      i=n=500;          
      break; 
     } 
     else if ((i==(3+(n*4)))&&i<(total-5)&&(array[i]==0)) 
     { 
      array[i]=1; 
      i=n=500; 
      break; 
     } 
     else if (i==(total)&&(array[i]==0)) 
     { 
      array[i]=1; 
      i=n=500; 
      break; 
     } 
     else if (i==(total-1)&&(array[i]==0)) 
     { 
      array[i]=1; 
      i=n=500; 
      break; 
     } 
     else 
      printf("**Something**"); 
    } 
    } 
} 
+1

存在C.沒有特殊的'別人if'它只是一個組合。 –

+0

只是將你最後的'else'語句替換爲'else if',你應該沒問題。 **'c' **沒有像其他語言那樣隱含'else if'語句**(例如:Python:'elif')**。 –

回答

0

只需卸下最後一個「其他」,取而代之的是另一種「否則,如果」

for (i=0; i<(total); i++) 
    { 
    for (n=0; n<((total)/4); n++) 
    { 
     if (i==(0+(n*4)) && i<(total-5)&&(array[i]==0)) 
     { 
      array[i]=1; 
      i=n=500;          
      break; 
     } 
     else if ((i==(3+(n*4)))&&i<(total-5)&&(array[i]==0)) 
     { 
      array[i]=1; 
      i=n=500; 
      break; 
     } 
     else if (i==(total)&&(array[i]==0)) 
     { 
      array[i]=1; 
      i=n=500; 
      break; 
     } 
     else if (i==(total-1)&&(array[i]==0)) 
     { 
      array[i]=1; 
      i=n=500; 
      break; 
     } 
     else if 
      printf("**Something**"); 
    } 
    } 
}