本文介紹了For 循環(huán)打印一個(gè)額外的逗號(hào)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問題描述
cout<<"Set B : {";
for(i=0;i<b;i++)
{
cout<<setB[i];
cout<<",";
}
cout<<" }"<<endl;
上面的代碼打印不正確.它應(yīng)該打印 Set B : {1,2,3} 但它打印一個(gè)額外的逗號(hào) ==> Set B : {1,2,3,}
The code above is not printing right. It should print Set B : {1,2,3} but it prints an extra comma ==> Set B : {1,2,3,}
任何幫助將不勝感激.提前致謝!
Any help would be appreciated. Thanks in advance!
推薦答案
使用
cout << "Set B : {";
for (i = 0; i < b; ++i) {
if (i > 0) cout << ",";
cout << setB[i];
}
cout << " }" << endl;
我改變了你的算法:
之前的意思是:先輸入數(shù)字再輸入逗號(hào)"
Before it meant : "Put the number and then put a comma"
現(xiàn)在的意思是:如果我身后有一個(gè)數(shù)字,就用逗號(hào),然后把數(shù)字"
Now it means : "If there is a number behind me put a comma, then put the number"
以前,您總是在打印數(shù)字時(shí)打印一個(gè)逗號(hào),因此您有一個(gè)額外的逗號(hào).
Before, you always printed a comma when you printed a number so you had an extra comma.
這篇關(guān)于For 循環(huán)打印一個(gè)額外的逗號(hào)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!