这是我在学校编程类(class)的作业题,我有点迷路了,所以请帮忙。这是问题:
Write an application program that prints a diamond of asterisks (*) on the screen. The number of lines in the diamond is given by the user. As an example, if the user requested a diamond with 7 lines, the following would be displayed.
这是我目前所拥有的:
{
int nlines;
int nsp;
cout << "enter the number of lines (must be more then one)" << endl;
cin >> nlines;
while((nlines)<=0)
{
cout << "enter the number of lines (must be more then one)" << endl;
cin >> nlines;
}
nsp=(nlines - 1)/2;
for(int currspace = 1; currspace <= nsp; currspace++)
{
cout << " ";
}
cout << "*" << endl;
for( int currentline = 0; currentline < nlines; currentline++)
{
for( int currentaster = 0; currentaster <= currentline; currentaster++)
cout << "*";
cout << endl;
}
return 0;
最佳答案
我不会尝试给出完整的答案,因为它是家庭作业,但我的直接建议是尝试将任务分解为一些子任务,每个子任务都比较简单。例如,我会从一个小函数开始,它只打印出 一 行指定长度的星号和指定的理由。从事情的表面上看,您可能可以很容易地处理这些事情,并且有了它,剩下的就更简单了。
关于c++ - 打印 ASCII 艺术钻石,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1541487/