2016-10-04 102 views
-2

我想這轉化爲循環做while循環,並保持一個7 x 7矩陣轉換爲用於循環做while循環

for循環輸出數量7 x 7

for (int height = 0; height < 7; height++){ 
    cout << numberMatrix[height][digitOne] << " "; 
    cout << numberMatrix[height][digitTwo] << " "; 
    cout << numberMatrix[height][digitThree] << " "; 
    cout << endl; 
} 

右輸出:

enter image description here

這是我的代碼轉換後,但它不出來的權利。

for循環輸出數量7 x 7

int height = 0; 
    while (height < 7) { 
     cout << numberMatrix[height][digitOne] << " "; 
     cout << numberMatrix[height][digitTwo] << " "; 
     cout << numberMatrix[height][digitThree] << " "; 
     height++; 
    } 
} 

錯誤輸出:

enter image description here

+4

'COUT << ENDL;'你忘記在while循環 – sinsuren

+0

請不要張貼圖片,複製/粘貼文本輸出 – Garf365

+0

到兩個代碼段這條線是等價的(除了缺少'cout << endl') – bolov

回答

0

在這種情況下,你應該使用時/ for循環。 只有在需要至少一次循環時才應使用循環,即使條件爲假,也應評估循環。

不過你可以試試這個do-while

int height = -1; 
do{ 
    if(height > 0){ 
     cout << numberMatrix[height][digitOne] << " "; 
     cout << numberMatrix[height][digitTwo] << " "; 
     cout << numberMatrix[height][digitThree] << " "; 
     cout << endl; 
    } 
    height++; 
}while(height < 7); 
+0

爲什麼'int height = -1;'?它在第一次迭代中將不會打印任何內容,並且您將添加一個額外的無用「if」。 –