gpt4 book ai didi

C++ Diamond 控制台输出问题

转载 作者:行者123 更新时间:2023-11-28 07:45:23 24 4
gpt4 key购买 nike

我正在尝试让我的 for 循环在给定用户特定最大值和最小值的情况下输出菱形,即使输入也是不允许的。任何想法,将不胜感激。练习并不像我想象的那么顺利。

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

int rows, count;

cout<<"What is the width of the diamond (3 to 21, odd values only): ";
cin>>rows;

//Error checking
if(rows < 1 || rows > 25)
{
cout<<"Invalid. please enter an odd number from 1 to 25: ";
cin>>rows;
}

//Ascending
for (count = 1; count < rows; count += 1)
{
for (int rows = 0; rows < count; rows ++)
cout<<"*";
cout<<endl;
}

//Descending
for (count; count > 0; count -= 1)
{
for (int rows = 0; rows < count; rows ++)
cout<<"*";
cout<<endl;
}


cin.get();
cin.get();

return 0;

}

最佳答案

这执行了目标的前半部分:

for (int stars = 1; stars <= rows; stars+=2)
{
int padding = rows - stars;
for (int c = 0; c < padding/2; ++c)
cout<<" ";
for (int s = 0; s < stars; ++s)
cout<<"*";
cout<<endl;
}

关于C++ Diamond 控制台输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14990338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com