c++ - 打印与数组中的值一样多的星星

标签 c++ arrays nested-loops

我试图让一个 C++ 程序开始创建一个数组并从用户那里获取值,然后打印每个值 + 星号。示例:用户输入了 5 那么输出必须是这样的这 5***** 输入

1

2

3

4

5

6

输出

1*

2**

3***

4**** 等等

.. 帮助:(

#include <iostream> 
using namespace std; 
void main() 
{
    int arr[10]; 
    for (int i = 0; i < 10; i++)
    {
        cin >> arr[i]; 
        int x = arr[i]; 
        for (int j = 0; x <= arr[i]; j++)
        {
            cout<< "*";
        }
    }
}

还有另一个帮助,你能给我一些有用的链接来练习编程成为专业的

最佳答案

您的代码有误。使用以下代码:

#include <iostream> 
using namespace std; 
int main()  {
  int arr[10]; 
  for (int i = 0; i < 10; i++)
  {
   cin >> arr[i]; 
   int x = arr[i]; 
   for (int j = 0; j < x; j++){ // your condition was wrong

   cout<< "*";
  }
   cout<<endl; // for better formatting
 }
 return 0;
}

对于已编辑的问题

int main()  {
int arr[10];
for (int i = 0; i < 10; i++)
{
    cin >> arr[i];


}
for (int i = 0; i < 10; i++)
{
    int x = arr[i];
    cout << x;
    for (int j = 0; j < x; j++){ // your condition was wrong

        cout << "*";
    }
    cout << endl;
}

return 0;
} 

关于c++ - 打印与数组中的值一样多的星星,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658622/

相关文章:

c++ - 返回 std::shared_ptr 的 vector 是否安全?

c++ - 比较双向链表中的指针?

c++ - 为什么 Visual Studio Performance Profiler 不显示帧时间和 FPS 的任何内容?

c++ - 函数返回 C++ 的段错误

javascript - 捕获括号 -/(\d)/?或/\s*;\s*/?

java - 如何创建一个使数字右侧对齐的java程序?

python - 如何在python中将向量附加到矩阵

.net - 在 python 数组和 .NET 数组之间转换

python - PyParsing 解析带有大括号和特定 header 的嵌套循环

python - 修复 Python 中的 4 个嵌套 for 循环