C++ C++;高度及;矩形宽度在3到20之间,程序检查是否存在非法条目
我试图写一个程序,允许用户选择高度和宽度的矩形(3和20之间)与任何给定的字符。我需要输入非法条目,允许在输入超出范围时保持尝试输入。下面的程序可以工作,但只能在小于3或大于20的条件下工作。我怎样才能让这个程序在两种条件下都工作C++ C++;高度及;矩形宽度在3到20之间,程序检查是否存在非法条目,c++,C++,我试图写一个程序,允许用户选择高度和宽度的矩形(3和20之间)与任何给定的字符。我需要输入非法条目,允许在输入超出范围时保持尝试输入。下面的程序可以工作,但只能在小于3或大于20的条件下工作。我怎样才能让这个程序在两种条件下都工作 #include <iostream> #include <iomanip> using namespace std; int main() { int height, width; char ch; do {
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int height, width;
char ch;
do {
cout<<"Enter desired height (3 to 20): ";
cin>>height;
if (height>3)
cout<<"";
else if (height<3)
cout<<"Illegal entry."<<endl;
} while (height<3);
do {
cout<<"Enter desired width (3 to 20): ";
cin>>width;
if (width>3)
cout<<"";
else if (width<3)
cout<<"Illegal entry. "<<endl;
} while (width<3);
cout<<"What character would you like for your rectangle? ";
cin>>ch;
for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {
cout<<ch;
}
cout<<endl;
cout<<endl;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int高度、宽度;
char ch;
做{
库尔特;
如果(高度>3)
cout我如何让这个程序在两种条件下都工作
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int height, width;
char ch;
do {
cout<<"Enter desired height (3 to 20): ";
cin>>height;
if (height>3)
cout<<"";
else if (height<3)
cout<<"Illegal entry."<<endl;
} while (height<3);
do {
cout<<"Enter desired width (3 to 20): ";
cin>>width;
if (width>3)
cout<<"";
else if (width<3)
cout<<"Illegal entry. "<<endl;
} while (width<3);
cout<<"What character would you like for your rectangle? ";
cin>>ch;
for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {
cout<<ch;
}
cout<<endl;
cout<<endl;
}
return 0;
}
您想使用逻辑and和逻辑or
if ( (height>3) && (height < 20) )
cout << "";
else if (height<3 || height > 20)
cout << "Illegal entry." << endl;
if((高度>3)和&(高度<20))
难道我对键入“缩进你的代码”感到厌烦了吗?也要始终使用大括号-你不希望被人抓住,你的裤子缠在脚踝上。我只缩进了我需要的东西,以便将其发布。缩进在我的程序中是合适的。你只需要一个AND子句。“如果高度>3,高度<20,那么…”。在逻辑运算符下查找。哦,实际上,您希望>=,而不是>;3是合法值,您需要将其视为有效值。我尝试在(width>=3&&width另一件事,与您的问题无关,但与@EdHeal的评论有关:由于括号错误,您在矩形图形中返回0,您将永远不会得到多行。谢谢。我对这两种情况使用了&&statements,而不是| |。我一更改它,程序就运行得很好。谢谢!!