当用户输入有值时,else 条件以某种方式被绕过
else condition get's somehow bypassed when the user input has a value
所以我正在尝试制作一个反复要求用户猜测数字的程序。如果这些条件之一为真,他就会输(或程序退出):
- 用户使用号码 5
- 用户使用大于 10 的数字
- 用户使用的数字等于他已经猜到的次数
- 用户只有 10 次尝试
这是问题所在,如果我输入数字 5 作为我的第一个猜测,则 else 函数起作用并且 cout 输出字符串 但是 如果我首先猜测其他内容然后使用数字 5 由于某种原因,程序只是退出而没有在最后一个 else 块中输出字符串。
这是我希望看到一些关于我需要更改的反馈,以便我可以输入多个用户输入,然后 然后 仍然使用数字 5 来获得 cout else 函数中的字符串。提前谢谢你:)
代码如下:
#include <iostream>
using namespace std;
int main() {
int input = 0;
short counter = 0;
cout << "Hello fellow PC-User why don't you guess a number that is between 1-10 ?" << endl;
cout << "Come on give it a try!\nOnly two conditions (maybe three) don't use the Number 5 and only Numbers 1-10!\nNow let's go!!!" << endl;
cin >> input;
if(input != 5 && input < 10){
while(input != 5 && input < 10) {
++counter;
cout << "Nice you guessed " << counter << flush;
if (input > 1){
cout <<" times!" << flush;
} else{
cout << " time!" << flush;
}
cout << "\nNow go at it again just don't used the number the times u tried bro!" << endl;
cin >> input;
if(counter == 10){
cout << "Damn that motivation doe...well what can I say\n I give up \n U win." << endl;
return 0;
} else if(counter == input){
cout << "Don't do it..." << endl;
return 0;
}
}
} else {
cout << "Yo! You weren't supposed to use that number!\nNow u looooose" << endl;
}
return 0;
}
outer if 条件确实不需要,因为它只会在第一次执行。只需在 while 循环之后移动 if-else 部分,看看会发生什么。实际上根本不需要 if 条件,因为只有当输入 == 5 或输入 > 10 时才会退出 while 循环。而且你还需要处理负数。
块的 else
部分不会在 if
块内的 while
循环中被调用。
如果您的第一个输入是正确的,即
中的条件
if(input != 5 && input < 10){
计算为 true
,else
部分永远不会执行。如果您希望无论 if
语句的条件何时计算,都执行 else
块,则需要以不同的方式重新组织 if
块和 while
循环为假。
// Simple conditional.
// Loop forever until a break statement in the loop breaks the loop.
while(true ) {
if(input != 5 && input < 10){
++counter;
cout << "Nice you guessed " << counter << flush;
if (input > 1){
cout <<" times!" << flush;
} else{
cout << " time!" << flush;
}
cout << "\nNow go at it again just don't used the number the times u tried bro!" << endl;
cin >> input;
if(counter == 10){
cout << "Damn that motivation doe...well what can I say\n I give up \n U win." << endl;
return 0;
} else if(counter == input){
cout << "Don't do it..." << endl;
return 0;
}
} else {
cout << "Yo! You weren't supposed to use that number!\nNow u looooose" << endl;
// Provide a way to break out of the while loop.
break;
}
}
所以我正在尝试制作一个反复要求用户猜测数字的程序。如果这些条件之一为真,他就会输(或程序退出):
- 用户使用号码 5
- 用户使用大于 10 的数字
- 用户使用的数字等于他已经猜到的次数
- 用户只有 10 次尝试
这是问题所在,如果我输入数字 5 作为我的第一个猜测,则 else 函数起作用并且 cout 输出字符串 但是 如果我首先猜测其他内容然后使用数字 5 由于某种原因,程序只是退出而没有在最后一个 else 块中输出字符串。
这是我希望看到一些关于我需要更改的反馈,以便我可以输入多个用户输入,然后 然后 仍然使用数字 5 来获得 cout else 函数中的字符串。提前谢谢你:)
代码如下:
#include <iostream>
using namespace std;
int main() {
int input = 0;
short counter = 0;
cout << "Hello fellow PC-User why don't you guess a number that is between 1-10 ?" << endl;
cout << "Come on give it a try!\nOnly two conditions (maybe three) don't use the Number 5 and only Numbers 1-10!\nNow let's go!!!" << endl;
cin >> input;
if(input != 5 && input < 10){
while(input != 5 && input < 10) {
++counter;
cout << "Nice you guessed " << counter << flush;
if (input > 1){
cout <<" times!" << flush;
} else{
cout << " time!" << flush;
}
cout << "\nNow go at it again just don't used the number the times u tried bro!" << endl;
cin >> input;
if(counter == 10){
cout << "Damn that motivation doe...well what can I say\n I give up \n U win." << endl;
return 0;
} else if(counter == input){
cout << "Don't do it..." << endl;
return 0;
}
}
} else {
cout << "Yo! You weren't supposed to use that number!\nNow u looooose" << endl;
}
return 0;
}
outer if 条件确实不需要,因为它只会在第一次执行。只需在 while 循环之后移动 if-else 部分,看看会发生什么。实际上根本不需要 if 条件,因为只有当输入 == 5 或输入 > 10 时才会退出 while 循环。而且你还需要处理负数。
块的 else
部分不会在 if
块内的 while
循环中被调用。
如果您的第一个输入是正确的,即
中的条件if(input != 5 && input < 10){
计算为 true
,else
部分永远不会执行。如果您希望无论 if
语句的条件何时计算,都执行 else
块,则需要以不同的方式重新组织 if
块和 while
循环为假。
// Simple conditional.
// Loop forever until a break statement in the loop breaks the loop.
while(true ) {
if(input != 5 && input < 10){
++counter;
cout << "Nice you guessed " << counter << flush;
if (input > 1){
cout <<" times!" << flush;
} else{
cout << " time!" << flush;
}
cout << "\nNow go at it again just don't used the number the times u tried bro!" << endl;
cin >> input;
if(counter == 10){
cout << "Damn that motivation doe...well what can I say\n I give up \n U win." << endl;
return 0;
} else if(counter == input){
cout << "Don't do it..." << endl;
return 0;
}
} else {
cout << "Yo! You weren't supposed to use that number!\nNow u looooose" << endl;
// Provide a way to break out of the while loop.
break;
}
}