C++ 而循环不';t形环
在这里,我设置了一个while循环,在其中输入选择编号1或2。如果我选择1或2,它就等于我选择的1或2;如果我选择一个低于1或高于2的数字,它将转到else语句,要求用户再次输入。但是如果用户输入,问题是如果他们选择正确的数字1或2,它将不会循环回到开始;它只会在那里结束循环,而不会转到正确的C++ 而循环不';t形环,c++,while-loop,if-statement,C++,While Loop,If Statement,在这里,我设置了一个while循环,在其中输入选择编号1或2。如果我选择1或2,它就等于我选择的1或2;如果我选择一个低于1或高于2的数字,它将转到else语句,要求用户再次输入。但是如果用户输入,问题是如果他们选择正确的数字1或2,它将不会循环回到开始;它只会在那里结束循环,而不会转到正确的if子句。为什么会这样?我觉得好像我是对的 int menus (int selection) { while ( selection > 2 || selection < 1)
if
子句。为什么会这样?我觉得好像我是对的
int menus (int selection)
{
while ( selection > 2 || selection < 1)
{
if (selection == 1)
{
cout << " 1 " << endl;
}
if (selection == 2)
{
cout << " 2 " << endl;
}
else
cout << "You didnt choose 2 or 3. Choose again";
cin >> selection;
}
return 0;
}
int菜单(int选择)
{
而(选择>2 | |选择<1)
{
如果(选择==1)
{
仔细看看你的while条件:只有当selection大于2或小于1时,它才会循环。然后你检查selection是1还是2,这是你刚刚排除的。
循环中也没有输入命令。仔细查看您的“while”条件:只有当“selection”大于2或小于1时,它才会循环。然后检查选择是1还是2—您刚才排除的。
循环中也没有输入命令。您的while循环排除了要在循环中测试的值
while ( selection > 2 || selection < 1)
{
if (selection == 1)
{
// Impossible!! selection isn't > 2 or < 1.
}
while(选择>2 | |选择<1)
{
如果(选择==1)
{
//不可能!!选择不是>2或<1。
}
您可以这样做:
while (1)
{
switch (selection) {
case 1:
case 2:
cout << " " << selection << " " << endl;
return 0;
default:
cout << "You didnt choose 1 or 2. Choose again";
cin >> selection;
}
}
while(1)
{
开关(选择){
案例1:
案例2:
cout您的while循环排除了要在循环内测试的值
while ( selection > 2 || selection < 1)
{
if (selection == 1)
{
// Impossible!! selection isn't > 2 or < 1.
}
while(选择>2 | |选择<1)
{
如果(选择==1)
{
//不可能!!选择不是>2或<1。
}
您可以这样做:
while (1)
{
switch (selection) {
case 1:
case 2:
cout << " " << selection << " " << endl;
return 0;
default:
cout << "You didnt choose 1 or 2. Choose again";
cin >> selection;
}
}
while(1)
{
开关(选择){
案例1:
案例2:
cout您的循环条件被扭曲
只有当选择为3、4、5、…(因为selection>2
)或selection
为0、-1、-2、…(因为selection<1
)时,才能通过循环。因此,如果
,则永远不会进入任何一个;您将转到其他
。然后,在重新执行循环之前,您将无条件地输入一个新选择。如果现在输入有效值,则循环将终止。您还可能会注意到,您的消息与您希望用户提供的内容不匹配
正确缩进后的代码与此非常相似:
int menus (int selection)
{
while (selection > 2 || selection < 1)
{
if (selection == 1)
{
cout << " 1 " << endl;
}
else if (selection == 2)
{
cout << " 2 " << endl;
}
else
cout << "You didn't choose 1 or 2. Choose again: ";
cin >> selection;
}
return 0;
}
int菜单(int选择)
{
而(选择>2 | |选择<1)
{
如果(选择==1)
{
cout您的循环条件被扭曲
只有当选择为3、4、5、…(因为selection>2
)或selection
为0、-1、-2、…(因为selection<1
)时,才能通过循环。因此,如果
,则永远不会进入任何一个;您将转到其他
。然后,在重新执行循环之前,您将无条件地输入一个新选择。如果现在输入有效值,则循环将终止。您还可能会注意到,您的消息与您希望用户提供的内容不匹配
正确缩进后的代码与此非常相似:
int menus (int selection)
{
while (selection > 2 || selection < 1)
{
if (selection == 1)
{
cout << " 1 " << endl;
}
else if (selection == 2)
{
cout << " 2 " << endl;
}
else
cout << "You didn't choose 1 or 2. Choose again: ";
cin >> selection;
}
return 0;
}
int菜单(int选择)
{
而(选择>2 | |选择<1)
{
如果(选择==1)
{
不能使用
do{
}而(选择>2 | |选择<1);
代替while。使用
do{
}而(选择>2 | |选择<1);
而不是while。调用int menums()
内部其他类似的命令:
else{
cout << "You didnt choose 2 or 3. Choose again";
cin >> selection;
menus(selection);
}
else{
cout>选择;
菜单(选择);
}
调用int menus()
内部其他类似内容:
else{
cout << "You didnt choose 2 or 3. Choose again";
cin >> selection;
menus(selection);
}
else{
cout>选择;
菜单(选择);
}
如果输入1或2,它甚至不会进入循环,因为1或2都不大于2或小于1,它们是相等的
这应该可以解决这个问题:
while ( selection >= 2 || selection <= 1) //equals while(true)
while(selection>=2 | | selection如果输入1或2,它甚至不会进入循环,因为1或2都不大于2或小于1,它们是相等的
这应该可以解决这个问题:
while ( selection >= 2 || selection <= 1) //equals while(true)
while(selection>=2 | | selection如果他们选择了一个有效的数字,那么这个数字就不再满足循环的条件要求。如果用户失败太多次,你会发现在bool上循环会更容易,你可以设置,否则,你可以在(selection==1 | | selection==2)
当数字无效时将停止循环如果他们选择了一个有效的数字,该数字将不再满足循环的条件要求。如果用户失败次数太多,您可以在bool上设置循环,否则,您可以使用do{/*获取输入,计数或要求他们修复它*/}而(selection==1 | | selection==2)
当数字无效时将停止循环在我看来,您正在将选择传递到函数中。
仅当输入不是1或2时,while循环才会运行
因此,将代码更改为:
int menus (int selection)
{
while (true)
{
if (selection == 1 || selection == 2)
{
cout << selection << endl;
break;
}
else
{
cout << "You didnt choose 1 or 2. Choose again";
cin >> selection;
}
}
return 0;
}
int菜单(int选择)
{
while(true)
{
如果(选择==1 | |选择==2)
{
cout在我看来,您正在将选择传递到函数中。
仅当输入不是1或2时,while循环才会运行
因此,将代码更改为:
int menus (int selection)
{
while (true)
{
if (selection == 1 || selection == 2)
{
cout << selection << endl;
break;
}
else
{
cout << "You didnt choose 1 or 2. Choose again";
cin >> selection;
}
}
return 0;
}
int菜单(int选择)
{
while(true)
{
如果(选择==1 | |选择==2)
{
cout和so“selection”永远不会更改。如果while
条件的计算结果为true
,您将有一个无休止的循环。它必须在循环期间更改。因此输入命令必须在您的循环内。因此我将更改selection>2 | | selection<1部分?首先,selection
的值必须在t内更改循环-因此输入命令需要在循环中。其次,是的,您需要考虑一下正在评估的条件。因此“选择”永远不会改变。如果while
条件