Console.WriteLine(" {0}{1}{0}", new string('*', n * 2 - 2), new string(' ' , n + 1));
如何用其他内容替换代码末尾的空格。
我可以做这样的吗?
new string(if ( i == n/2) {'-'} else {' '}
最佳答案
您正在寻找conditional ternary operator .
new string((i == n/2) ? '-' : ' ', n + 1);
您也可以使用普通的if...else
:
char character;
if (i == n/2)
character = '-';
else
character = ' ';
Console.WriteLine(" {0}{1}{0}", new string('*', n * 2 - 2), new string(character , n + 1));
关于c# - 如何替换 "new string"方法中的符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31490227/