C#中布尔问题 [英] Problems with booleans in C#

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

问题描述

为什么这段代码不起作用?似乎swtitch bool thingy出了问题,因为我什么都没有。当我运行程序时(这不是整个程序,如果你需要它来解决问题请注释)并完成所有级别,包括老板级别我只是得到级别菜单标题但没有别的。



感谢您的帮助!



BTW抱歉使用gotos请不要激怒我

BTW 2我是全新的,2天前用c#开始。



Why does this code not work? It seems that something is wrong with the swtitch bool thingy because I dont anything. When I run the program (this isnt the whole program by the way please comment if you need it to solve the problem) and finish all levels including the boss level I just get the Level Menu heading but nothing else.

Thanks for your help!

BTW sorry for using gotos please dont flame me
BTW 2 I am completely new and started 2 days ago with c#.

if (num001 == answer01)
            {

                Console.Write("What is " + num19 + " plus " + num20 + "?: ");
                int answer = Convert.ToInt32(Console.ReadLine());
                if (answer == num19 + num20)
                {

                    Console.WriteLine("Well done! Your answer is correct. \nCONGRATS! You mastered the first level \nPress any key to go to the Level selection");
                    l1finished = !l1finished;
                    Console.ReadKey();
                    

                    goto LevelMenu;


                }
                else
                {

                    Console.WriteLine("Are you even trying?");
                    Console.ReadKey();
                    goto Menu;

                }
            }


        LevelMenu:
            Console.WriteLine("                                                       Level Menu");
            for (int i = 1; i <= 27; i++)
            {
                Console.WriteLine();
            }
            int answer02 = Convert.ToInt32(Console.ReadLine());
            if (l1finished == true)
            {
                Console.WriteLine("Press 1 to enter the Main Menu. Press 2 to enter next Level");
                if (answer02 == 1)
                {
                    goto Menu; //I know gotos aren't the best. Plaese leave me!
                }
                if (answer02 == 2)
                {
                    goto Menu; //Level 2 doesn't exist yet. Please don't flame me!
                }

                else
                {
                    Console.WriteLine("            You have to finish Level 1 first! Come back again later.");
                }
            }





我的尝试:



我也尝试了l1finished == true但这似乎也没有用。



What I have tried:

I also tried the l1finished == true but that also seems to be not working.

推荐答案

引用:

BTW抱歉使用gotos请不要激怒我

BTW sorry for using gotos please dont flame me



哈 - 两天你已经知道每个人最大的错误 - 承担;)



这里的切换:l1finished =!l1finished;没必要。只需使用l1finished = true;



在'LevelMenu:'段中,你写出Level Menu+ 27条清晰的行,但是你在检查之前要求输入如果等级结束:

int answer02 = Convert.ToInt32(Console.ReadLine());



控制台将等待在进一步移动之前输入。


Ha - two days and you already know everyone's biggest bug-bear ;)

The toggle here: l1finished = !l1finished; is unnecessary. Just use l1finished = true;

In the 'LevelMenu:' segment, you write out the "Level Menu" + 27 clear lines but you ask for an input before checking if the level is finished:
int answer02 = Convert.ToInt32(Console.ReadLine());

The console will wait for an input before moving any further.


感谢goto,如果没有整个程序,就不可能知道你的代码做了什么,唯一的建议就是学会使用调试器。



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



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

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



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

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个错误。
Thanks to the goto's, it is impossible to know what your code do without the whole program, the only advice is to learn to use the debugger.

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.


读取输入尝试如下:



to read input try like this:

int op = 0;
string in = string.Empty;
do
{
  Console.WriteLine("enter choice");
  in = Console.ReadLine();
} while (!int.TryParse(in, out op));


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

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