为什么在满足if语句之后,else语句仍然打印?
Why is the else statement still printing after the if statement is satisfied?
对不起,我是新的stackoverflow,但我有一个问题,而编码。我创建了这个简单的程序,但我注意到,它仍然打印else语句后,它与if语句。代码是用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!
我很确定你想要else if
(即嵌套)而不是(后续)if
s,但这只是一个猜测,因为你没有提供输入或输出。
我并不经常认为发布准确的代码是最好的教育方式,但在这种情况下,我认为区别会跳出来:
if(check == 'D')
{
cout<<"You pressed D"<<endl;
}
else if(check == 'S')
{
cout<<"You pressed S"<<endl;
}
else if(check == 'F')
{
cout<<"You pressed F"<<endl;
}
else
cout<<"You suck!";
确保你理解if
、else if
和else
之间的区别。
这也是使用switch
语句的标准情况。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 为什么这个 IF 语句在循环内不打印
- 二叉搜索树使用最常用的节点来打印出某些语句
- 自动打印出哪个 EXPECT 语句失败
- 我的 if 语句打印在我的 while 循环中的每个 if 语句上,无论它是否为假
- 所有类实例方法的打印语句最后都在打印吗?
- 如何在没有打印语句的情况下报告用户输入错误
- 面临在 if 语句之外打印变量数据的问题 完成使用 Qt 编程
- "1L << count"是什么意思?如何使用超出"unsigned long long int? "范围的语句编号打印?
- 用于打印每个操作数的单独语句
- 为什么返回语句和打印语句返回不同的答案?
- printf 语句在调试期间未在控制台上打印
- 仅禁止显示实际的 make 命令和最终打印语句
- 我的 for 循环之后的代码不起作用,for 循环后的打印语句不会被打印
- 在C 中使用继续语句的打印字符
- C++ to_string函数根据额外打印语句的先见之明,为浮点数和整数转换提供不同的结果
- 删除"超出矢量误差范围"的打印语句后输出不同的根本原因
- 打印此矩阵的 if 语句是什么
- 仅在向量末尾打印新行 - "if"语句在"for"循环之外