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时,会有不需要的空间
int sBasamak(int b)
{
int a=0;
while(b > 0)
{
a++;
b /= 10;
}
return a;
}
这是我为保持位数而做的,但解决方案的另一部分超出了我的智慧,希望你明白我想要尝试的东西。
编辑:(如果有人在这里,我如何修复它)
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";
答案 0 :(得分:-1)
如果您的问题是您想要'#' - es对齐,那么我会推荐以下两种方法之一:
\t
)代替空格(自动allignes本身)或if(n>=10)cout<</*x spaces*/
;否则cout&lt; x-1 spaces /;)希望这有帮助。
编辑: 或者,您可以输出ascii退格。不确定它的效果如何。