2015-11-09 164 views
0

如果我有這樣的循環:在for循環之外使用變量。

int i = 0; 
int bestscore = moveArray[2][0]; 
for (; i < count; i++) { 
    if (moveArray[2][i] > moveArray[2][0]) 
     bestscore = moveArray[2][i]; 
} 

printf("\nComputer places %c at %c%c.\n", color, (moveArray[0][i]) + 'a', (moveArray[1][i]) + 'a'); 

我想用外循環I值,所以這是我做的,但我相信有一個更好的方法通過使值的副本來做到這一點或者類似的東西?

+0

如果你想要的'最後一個值i'這是確定(在這種情況下,它會是'count') –

+0

什麼副本? '我'會在循環之後保留它的價值,它不在本地。 – ameyCU

+0

你的意思是更好的方法來做到這一點,你可以在循環中初始化'i'使它更漂亮,但這沒關係! –

回答

0

不知道你在問什麼,而是要找到可以使用的最好成績:

int i = 1; 
int bestscore = 0; 
for (; i < count; i++) { 
    if (moveArray[2][i] > moveArray[2][bestscore]) 
     bestscore = i; 
} 

printf("\nComputer places %c at %c%c.\n", color, (moveArray[0][bestscore]) + 'a', (moveArray[1][bestscore])