对不起,我是stackoverflow的新手,但我在编码时遇到了问题.我创建了这个简单的程序,但我注意到它在使用if语句完成后仍然打印了else语句.代码是用c ++编写的,非常感谢您的帮助.
# include <iostream>
using namespace std;
int main()
{
char check;
bool done = false;
while(not done)
{
cout<<"Please enter one of the options provided below."<<endl;
cout<<"D = distance S = second F = first"<<endl;
cin>>check;
if(check == 'D')
{
cout<<"You pressed D"<<endl;
}
if(check == 'S')
{
cout<<"You pressed S"<<endl;
}
if(check == 'F')
{
cout<<"You pressed F"<<endl;
}
else
cout<<"You suck!";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
例如,当我按D时,我只想接收You pressed D输出.相反,我得到了You pressed D You suck!