切换案例问题! ! ! [英] Switch case problem ! ! !

查看:91
本文介绍了切换案例问题! ! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我认为我的开关盒有问题,当我按1开始的情况下,但是当我按2它应该但它不是什么问题!!



我的尝试:



Basically, i think i have problem with switch case when i press 1 the case begins but when i press 2 it should but it doesn't what is the problem !!

What I have tried:

	char answer;
		int choice;
		do
		{
			system("cls");
			system("Color F ");
			cout << "\t|  --------------------------------- |" << endl;
			cout << "\t|  Press 1 for Palying Game.    |" << endl;
			cout << "\t|  Press 2 Exit				   |" << endl;
			cout << "\t|  Enter Your Choice            |" << endl;
			cout << "\t|  -----------------------------|" << endl;
			cout << "\t   \t";
			cin >> choice;    //this 
			switch (choice)
			{
			case 1:
			{
				system("cls");
				system("color E");
				t.org();
			}
			break;
			case 2:
				default:
				break;
			}
		} while (choice == 2);
	}
	return 0;
}

推荐答案

嗯...这是一些奇怪的代码 - 并且严重缩进!你应该尝试使缩进正确,这使得阅读代码变得更加容易。如果你打算在一个案例代码周围使用大括号,那么就把它放在整个代码周围,而不仅仅是它的一点 - 这看起来是错误的并且令人困惑。

char answer;

int choice;

Um...that's some odd code - and badly indented! You should try to get the indentation right, it makes reading the code a lot, lot easier. And if you are going to use curly brackets around a case code, then put it round the whole code, not just a bit of it - that just looks wrong and is confusing.
char answer;
int choice;
do
    {
    system("cls");
    system("Color F ");
    cout << "\t|  --------------------------------- |" << endl;
    cout << "\t|  Press 1 for Playing Game.         |" << endl;
    cout << "\t|  Press 2 Exit                      |" << endl;
    cout << "\t|  Enter Your Choice                 |" << endl;
    cout << "\t|  --------------------------------- |" << endl;
    cout << "\t   \t";
    cin >> choice;    //this 
    switch (choice)
        {
        case 1:
            {
            system("cls");
            system("color E");
            t.org();
            break;
            }
        case 2:
        default:
            break;
        }
    } while (choice == 2);

看看它发生了多少更清楚了?

我还建议案例中的代码应该尽可能短 - 所以将玩游戏代码移动到单独的功能,只需在案例内调用。它使功能更小,更易于阅读,理解和维护。



现在...关于实际代码...

循环将执行一次,除非用户按2退出,在这种情况下它将再次循环。可能,你想要:

See how much clearer it is what is going on?
I'd also suggest that the code inside a case should be as short as possible - so move the "play game" code into a separate function, and just call that inside the case. It makes the function smaller and easier to read, understand, and maintain.

Now...about the actual code...
The loop will execute once, unless the user presses 2 to exit, in which case it will go round again. Probably, you want:

} while (choice != 2);

这可能会解决您的问题!

That may fix your problem!


问题是您的循环继续时继续而选择是2并且如果是其他的话会中断。



因此它可能应该是:

The problem is that your do while loop continues while choice is 2 and breaks if it is anything else.

So it should be probably:
do
{
    // ...
} while (choice != 2);


Quote:

当我按2它应该但它不会

when i press 2 it should but it doesn't

它应该但它不是什么?



你应该学会尽快使用调试器尽可能地,它是一个令人难以置信的学习工具。而不是猜测你的代码在做什么,现在是时候看到你的代码正在执行并确保它能达到预期的效果。



Debugger - 维基百科,免费的百科全书 [ ^ ]

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



调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。

It should but it don't what ?

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

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于切换案例问题! ! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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