为什么输出“win endl win”? [英] Why is the output "win endl win"?

查看:54
本文介绍了为什么输出“win endl win”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< iostream>

使用命名空间std;



int main()

{

int digit;



for(digit = 0; digit< 2; digit ++)



开关(数字< = 2)

{

案例0:

if(digit == 0)

cout<<draw<< endl;

休息;



案例1:

cout<< 赢<< endl;

休息;



案例2:

cout<< ; 输掉<< endl;

休息;

}



返回0;



}



我的尝试:



我认为输出应该是Draw endl Win...

解决方案

考虑一下(数字< = 2)手段。这是一个条件语句,它解析为true或false(仅为true或false)。

学习使用你的调试器,因为他是如何找到这种类型的bug。


做这个改变并查看输出

  cout<< digit<< endl; 
cout<<(数字< = 2 )<< endl;

开关(数字< = 2





您应该尽快学会使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]


#include <iostream>
using namespace std;

int main()
{
int digit;

for (digit=0; digit<2; digit++)

switch (digit<=2)
{
case 0:
if (digit==0)
cout <<"draw"<<endl;
break;

case 1:
cout << "win"<<endl;
break;

case 2:
cout << "lose"<<endl;
break;
}

return 0;

}

What I have tried:

I thought the output should be "Draw endl Win"...

解决方案

Have a think about what (digit<=2) means. This is a conditional statement which resolves to true or false (and only true or false).
Learn to use your debugger as his is how to find this type of bug.


Do this change and look at output

cout <<digit<<endl;
cout <<(digit<=2)<<endl;
switch (digit<=2)



You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


这篇关于为什么输出“win endl win”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆