我想创建一个程序,它使用两个for语句来显示下面显示的星号模式。编写一个简单的语句程序,显示星号
**
****
******
********
**********
我可以让他们用了不少的声明,但我只想使用其中的2,把它缩短 这是我有:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int row = 1;
int astrk = 1;
for (int row = 1; row < 2; row += 1)
{
for (int astrk = 1; astrk <= 2; astrk += 1)
cout << '*';
cout << endl;
}// end for
for (int row = 1; row < 2; row += 1)
{
for (int astrk = 1; astrk <= 4; astrk +=1)
cout << '*';
cout << endl;
}//end for
for (int row = 1; row < 2; row += 1)
{
for (int astrk = 1; astrk <= 6; astrk += 1)
cout << '*';
cout << endl;
}// end for
for (int row = 1; row < 2; row += 1)
{
for (int astrk = 1; astrk <= 8; astrk += 1)
cout << '*';
cout << endl;
}// end for
for (int row = 1; row < 2; row += 1)
{
for (int astrk = 1; astrk <= 10; astrk += 1)
cout << '*';
cout << endl;
}// end for
return 0;
}
帮助吗? :)
** **提示:你这里有五个环,每行一个。而且他们几乎没有循环,因为每个迭代只有一次。它们又有什么不同?只有'astrk'的上限,它会变成'2,4,6,8,10'。这是否意味着解决方案? – Beta