-4
cout << "---------------------------------------------------\n";
cout << "# Level:" << getLevel() << " #\n";
cout << "# Max Hp:" << getMaxHp() << " #\n";
cout << "# Hp:" << getHp() << " #\n";
cout << "# Attack Point:" << getAtkPoint() << " #\n";
cout << "# Defense:" << getDefense() << " #\n";
cout << "---------------------------------------------------\n";
我想要做這樣的事情,但是當返回值的位的數量比1更將有多餘的空格我如何能在C++
int sBasamak(int b)
{
int a=0;
while(b > 0)
{
a++;
b /= 10;
}
return a;
}
該刪除的空間是我做過什麼爲了保持數字的數字,但解決方案的另一部分超出了我的智慧,希望你明白我想嘗試的。
編輯:(如果有人WONDER這裏怎麼我固定它)
void space(int b,char gelen[])
{
int a=0;
while(b >0) {
a++;
b /= 10;
}
int c=0;
for(int i=0 ; gelen[i] ; i++)
{
c++;
}
cout << setw(51-c-a) << "#\n";
}
和
int a=getLevel();
int b=getMaxHp();
int c=getHp();
int d=getAtkPoint();
int e=getDefense();
cout << "---------------------------------------------------\n";
cout << "# Level:" << a ; space(a," Level:");
cout << "# Max Hp:" << b; space(b," Max Hp:");
cout << "# Hp:" << c; space(c," Hp:");
cout << "# Attack Point:" << d; space(d," Attack Point:");
cout << "# Defense:" << e; space(e," Defense:");
cout << "---------------------------------------------------\n";
你的意思是什麼「無用空間」?我懷疑這只是一個格式問題。你能否按照原樣顯示輸出並按你想要的方式顯示? – user463035818
[設置字段寬度](http://en.cppreference.com/w/cpp/io/manip/setw)和[justification](http://en.cppreference.com/w/cpp/io/manip /剩下)。 –
請提供[MVCE](https://stackoverflow.com/help/mcve)。 – tambre