抱歉,我是 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;
}
例如,当我按 D 时,我只想接收You pressed D
作为输出。相反,我得到You pressed D You suck!