我对编码非常陌生,并且遇到了一个带有if-else程序的奇怪障碍。我的代码中有很多注释来解释发生了什么,所以我只是发布代码,但基本问题是当我尝试简化嵌套语句时,我只执行一个条件(覆盖所有其他条件) )。例如,如果我想在“x = 4”时发生某些事情,那么只有那个条件被执行 - 即使它是假的!但是,如果我将其声明为“x< 5&& x> 3”,那么程序运行正常(适用于真假条件的执行),但这看起来很混乱 - 特别是因为我想要多个某些响应输入。
我搜索了网站但找不到这个具体问题,“类似问题”似乎也不适用。那么任何想法是怎么回事?有什么东西可以使程序只执行一个if-else语句并忽略所有其他语句,即使该语句是假的吗?
代码:问题在于结尾附近的两个elseif语句(用注释标记)。
#include <iostream>
using namespace std;
int main()
{//This a program using loop functions to have the user
//guess variable "guess" (which we'll make "5"), until the user gets it
//right, and provides hints to help the user out
cout<<"Can you guess the number I'm thinking? It's between 1 and 10.\n";
//Prompts the user to input a guess
int guess;
//Declares the variable that the user will input
cin>>guess;
//Before the loop begins, the user input a guess
cout<<"\n";
//This gives us a line break after the answer is submitted
while (guess != 5){//Guessing incorrectly starts the loop
//The program is going to keep asking the user to guess
//until the user guesses "5"
if (guess < 1 ){//The program will warn the user if they're out of range, here it's too low
cout<<"No, that's too low! Guess a number between 1 and 10.\n";
cin>>guess; //Allow the user to guess again
cout<<"\n"; //Add a line break after the input
}
else //Now, give responses for other conditions
if(guess > 10){//Warn the user if they guess too high
cout<<"Too high! Guess a number between 1 and 10.\n";
cin>>guess;
cout<<"\n";
}
else //Tell the user if they're getting close.
//Not sure why I can't simply say if "guess = 4,6"
//Doing so causes only this response to execute, ignoring
//the others, except the above ">" and "<" statements
//which is why I'm continung to use </> statements here
if(guess > 5 && guess < 7 || guess < 5 && guess > 3){
cout<<"VERY close! Keep trying.\n";
cin>>guess;
cout<<"\n";
}
else //Tell the user if they're sort of close
//Again, not sure why I can't just say "guess=3,7"
if(guess > 2 && guess < 4 || guess > 6 && guess < 8){
cout<<"Close, but no cigar. Try again.\n";
cin>>guess;
cout<<"\n";
}
else {//For all other responses, we output this
cout<<"Guess again.\n";
cin>>guess;
cout<<endl; //We need to end the loop here, as these are all the conditions
//This kept creating a line break. My assumption is that
//by virtue of ending the loop, a line break was created
}
}
if(guess = 5){//Outside the loop, we need to specify the correct answer
//because the loop is only working while the answer in incorrect
cout<<"Good job. "<<guess<<" is right!\n";
}
cin.get();//This keeps the program from closing after completion
}
//Done
答案 0 :(得分:1)
有一点是:
if(guess = 5){
应该是
if(guess == 5){
您的意思是logical comparison
而不是assignment
。
另一点是根据C++ Operator Precedence,&&
的优先级高于||
。您应该在逻辑条件中使用()
来清楚地表达您的逻辑:
if((guess > 2 && guess < 4) || (guess > 6 && guess < 8))
答案 1 :(得分:0)
谈论整数时,
(guess > 5 && guess < 7 || guess < 5 && guess > 3)
相当于
(guess == 6 || guess == 4)
因此,至少。重组如下:
while (guess != 5) {
if (guess < 1 ) {
}
else if (guess > 10)
}
else if (guess == 6 || guess == 4) {
}
else if (guess == 3 || guess == 7) {
}
else { // executes for 1, 2, 5 (then leaves loop), 8, 9, 10
}
}
你的意思是......
if (guess == 5) // equivalence check
不
if (guess = 5) // assignment and "if" becomes true (always)
答案 2 :(得分:0)
在C ++中,问if(x = whatever)
在语法上不正确。但是,它没有按照你的想法做到。
区别在于:
//is the value of x equal to 4?
if(x == 4)
//does the assignment of 4 to the variable x return true? (meaning it worked)`
if(x = 4)
所以正在发生的事情是你认为错误的事实。