c++ - 编写一个显示星号的简单 for 语句程序

标签 c++ for-loop

我想创建一个程序,它使用两个 for 语句来显示如下所示的星号模式。

 **
 ****
 ******
 ********
 **********

我可以让他们使用相当多的 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;
 }

请帮忙? :)

最佳答案

你应该有一个用于行数的外循环,以及一个用于星号的内循环。内循环打印完星号后,外循环打印换行符,并增加星号计数。

在伪代码中:

for (from one to the number of lines)
{
    for (one to the number of asterisks)
    {
        print an asterisk
    }

    print a newline
    increase number of asterisks
}

关于c++ - 编写一个显示星号的简单 for 语句程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13522771/

相关文章:

c++ - 是否使用 volatile 引用保证读取操作不会被重新排序?

c++ - 创建临时长双变量

GTest TYPED_TEST 的 C++ 多个参数

javascript - coffeescript while/loop/for multiple statements in beautiful coffee

c++ - C/C++多进程访问同一个变量的方法

c++ - 未定义/未指定?

javascript - 如何使用 JavaScript/jQuery 自动定期更改网页的标题?

python - 当 for 循环的变量有一段时间条件时,如何在 for 循环中获得步骤跳转

Java 阶乘程序

javascript - 在javascript中对多维数组进行排序?