帮助循环结构 [英] Help with Looping Structure
问题描述
我有一个编程任务,我一直在上课。基本上,该程序创建了+的设计。根据用户输入他或她想要显示的行数。这是一个基本的例子,如果有人要求有6行设计,输出可能是什么样的:
+
++
+++
+++
++
+
我可以在达到中段以取走+之后,不会得到递减循环。另外,我必须检查输入验证。用户必须选择的行数是2-14,不包括赔率。我的输入验证正确地挑出了不在范围内但不会阻止输入奇数的任何数字。这是我的代码。有人可以帮忙吗?
//这个程序显示某种+的模式。取决于用户的输入。
#include< iostream>
使用命名空间std;
int main()
{
int UserSel = 0;
int HalfValue = 0 ;
cout<< 在模式中你想要多少行(仅限偶数:2-14)? " ;;
cin>> UserSel;
//输入验证
if(UserSel< 2 || UserSel> 14 || UserSel%2!= 0)
cout<< \ n无效的行数。请重试。\ n \ nn ;;
else
{
HalfValue = UserSel / 2; //值是循环开始递减的地方
//代码开始+'的模式
for(int Row = 0; Row< = HalfValue; Row ++)
{
for(int Star = 0; Star< Row; Star ++)
{
cout< ;< " +";
}
cout<< " \\\
英寸;
}
//代码带来+'的左后方完成模式
for(int Row = HalfValue; Row!= 0;行 - )
cout<< " +";
}
cout<< " \ n";
返回0;
}
I have a programming assignment I''ve been working on for class. Basically the program creates a design of "+''s" based on user''s input of how many rows he or she would like displayed. Here is a basic example of what the output may look like if someone requested there be 6 rows of design:
+
++
+++
+++
++
+
I can''t get the decrementing loop, after it has hit mid section to take away the "+''s". Also, I must check for input validation. The number of rows the user has to choose from are 2-14, excluding odds. My input validation correctly singles out any numbers not in range but does not stop odd numbers from being entered. Here is my code. Can anyone help?
// This program displays a certain pattern of "+''s" depending on user''s input.
#include <iostream>
using namespace std;
int main ()
{
int UserSel = 0;
int HalfValue = 0;
cout << "How many rows do you want in the pattern (even numbers only: 2-14)? ";
cin >> UserSel;
// Input Validation
if (UserSel < 2 || UserSel > 14 || UserSel%2 != 0)
cout << "\nInvalid number of rows. Please retry.\n\n";
else
{
HalfValue = UserSel/2; // Value is where loop begins to decrement
//Code begins pattern of +''s
for (int Row = 0; Row <= HalfValue; Row++)
{
for (int Star = 0; Star < Row; Star++)
{
cout << "+";
}
cout << "\n";
}
//Code brings +''s back left to complete pattern
for (int Row = HalfValue; Row != 0; Row--)
cout << "+";
}
cout << "\n";
return 0;
}
推荐答案
我不确定如何在我的评论之后编写代码//代码带来+'的左后方完成模式。感谢任何可能提供帮助的人!
I''m not sure how to write the code after my comment // Code brings +''s back left to complete pattern. Thank you for any who might be able to help!
sonic,
与第一部分相同。
sonic,
The same way it does in the first part.
sicarie,感谢您的帮助。我想我已经做了你所解释的但是程序仍然不会打印出减少的+的完成模式。我认为这是一个简单的程序,结果令我难以置信。这是我的新代码:
sicarie, thanks for your help. I think I''ve done what you explained but program still won''t print out diminishing "+''s" to finish the pattern. What I thought would be an easy program has turned out mind-boggling me. Here''s my new code:
这篇关于帮助循环结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!