C#Xamarin使用If子句是否错误?

C#Xamarin使用If子句是否错误?,c#,if-statement,C#,If Statement,在我的C#代码中(我目前正在尝试学习),我试图制作一个基于文本控制台的冒险游戏。第一个“部分”是有效的,我复制粘贴了它,并更改了变量的名称等(这是一个重复的代码),但它就是不起作用 代码如下: using System; namespace LearningC { class MainClass { public static void Main (string[] args) { lvl_1_start:

在我的C#代码中(我目前正在尝试学习),我试图制作一个基于文本控制台的冒险游戏。第一个“部分”是有效的,我复制粘贴了它,并更改了变量的名称等(这是一个重复的代码),但它就是不起作用

代码如下:

using System;

namespace LearningC
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            lvl_1_start:                                                                                                                            //LEVEL ONE
            Console.WriteLine ("Welcome to AlexAdventureLand.\nPlease press the enter key to start.");
            Console.ReadKey ();
            Console.WriteLine ("You are a rabbit in a burrow. There is a badger outside. \nPress 1 to leave the burrow. \nPress 2 to wait.");
            int lvl_1 = Convert.ToInt32 (Console.ReadLine ());
            if (lvl_1 == 1) {
                Console.WriteLine ("The badger sees you and eats you. \nYou fail. \n\nPress enter to try again.");
                Console.ReadKey ();
                goto lvl_1_start;
            } else if (lvl_1 == 2) {
                Console.WriteLine ("The badger scurries away!");
                goto lvl_2_start;
            } else {
                Console.WriteLine ("You pressed the wrong button. Start again.");
                goto lvl_1_start;
            }
            lvl_2_start:                                                                                                                            //LEVEL TWO
            Console.WriteLine ("Now what do you want to do? \nPress 1 to leave burrow. \nPress 2 to wait.");
            Console.ReadKey ();
            int lvl_2 = Convert.ToInt32 (Console.Read ());
            if (lvl_2 == 1) {
                Console.WriteLine ("You leave the burrow and look around.");
                Console.ReadKey ();
                goto lvl_3_prt_a_start;
            } else if (lvl_2 == 2) {
                Console.WriteLine ("You wait a little longer. \nYou can hear footsteps around your burrow. You wonder whether it's your friend - troy - or the badger still.");
                goto lvl_3_prt_b_start;
            } else {
                Console.WriteLine ("You pressed the wrong button. Start again.");
                goto lvl_2_start;
            }
            lvl_3_prt_a_start:                                                                                                                      //LEVEL THREE PART A
            Console.WriteLine ("PlaceHolder");
            Console.ReadKey ();
            lvl_3_prt_b_start:
            Console.WriteLine ("PlaceHolder");
            Console.ReadKey ();

        }
    }
}

运行时会出现问题。第一部分工作正常,按“1”、“2”或任何其他数字都可以工作——但在第二部分,它总是出现else部分,没有if或if-else子句。任何快速的帮助都会很好。谢谢

问题是您同时调用和。它们各自读取一个字符,因此
ReadKey
获取您的选择,
read
仅获取您的
\r
(换行符的第一个字符)
ReadKey
返回有关按键的信息(被忽略),并且
Read
返回与您输入的
char
值相对应的
int
(请注意,
int
char
是数字类型,将其中一个
转换为nt32
与解析
字符串不同!)

您应该使用与工作代码类似的代码来读取输入:

int lvl_2 = Convert.ToInt32(Console.ReadLine()); // or, does the same thing:
int lvl_2 = int.Parse(Console.ReadLine());
(该类支持多种类型的转换;如果您只需将
字符串
解析为
int
,则该类是更常见的选项,并为您提供了更多选择)

此外,在C#中,您应该避免使用
goto
s。它们适合写作,总体来说是个坏主意(除了极少数例外)。我可以看出他们已经把你的方法变成了一个好方法。下面是一个例子,它将使您更加正确:

static void Main(string[] args)
{
    Level1();
}
static void Level1()
{
    Console.WriteLine("Welcome to AlexAdventureLand.\nPlease press the enter key to start.");
    Console.ReadKey();
    Console.WriteLine("You are a rabbit in a burrow. There is a badger outside. \nPress 1 to leave the burrow. \nPress 2 to wait.");
    int selection = int.Parse(Console.ReadLine());
    if (selection == 1)
    {
        Console.WriteLine("The badger sees you and eats you. \nYou fail. \n\nPress enter to try again.");
        Console.ReadKey();
        Level1();
    }
    else if (selection == 2)
    {
        Console.WriteLine("The badger scurries away!");
        Level2();
    }
    else
    {
        Console.WriteLine("You pressed the wrong button. Start again.");
        Level1();
    }
}
static void Level2()
{
    Console.WriteLine("Now what do you want to do? \nPress 1 to leave burrow. \nPress 2 to wait.");
    int selection = int.Parse(Console.ReadLine());
    if (selection == 1)
    {
        Console.WriteLine("You leave the burrow and look around.");
        Console.ReadKey();
        Level3A();
    }
    else if (selection == 2)
    {
        Console.WriteLine("You wait a little longer. \nYou can hear footsteps around your burrow. You wonder whether it's your friend - troy - or the badger still.");
        Level3B();
    }
    else
    {
        Console.WriteLine("You pressed the wrong button. Start again.");
        Level2();
    }
}
static void Level3A()
{
    Console.WriteLine("PlaceHolder");
    Console.ReadKey();
}
static void Level3B()
{
    Console.WriteLine("PlaceHolder");
    Console.ReadKey();
}

请不要使用goto!这是邪恶的!我很高兴你正在努力学习一门新语言。我要挑战你不要再使用goto命令。这不应该被使用。第二,将代码划分为多个方法,而不是一个超级方法。我认为问题可能是你读了两遍?在第一个示例中,您有Console.ReadKey();然后你再写一次,在下一次中,你读一次键,然后再读一次行。@McAdam331另外,第二级read使用“.read()”,而第一级read使用“.ReadLine()”我想你找到了它,@dodald。
static
意味着该方法属于类,而不是实例
void
表示该方法不返回任何内容。您可以查找这些关键字以了解更多详细信息。