我试图让一个 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/