C# Xamarin 使用了错误的 If 子句?

标签 c# if-statement

在我的 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 。这些每个读取一个字符,所以 ReadKey获取您的选择,然后 Read刚刚得到你的 \r (换行符的第一个字符)。 ReadKey返回有关您的按键的信息(被忽略),以及 Read返回 int对应char您输入的值(请注意 intchar 是数字类型,转换其中之一 ToInt32 与解析 string 不同!)。

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

int lvl_2 = Convert.ToInt32(Console.ReadLine()); // or, does the same thing:
int lvl_2 = int.Parse(Console.ReadLine());

( Convert 类支持多种类型的转换;如果您所需要做的就是将 string 解析为 int int.Parse 是更常见的选项,并为您提供更多选择)

此外,您应该避免 goto在 C# 中。他们适合写作spaghetti code ,总体来说是一个坏主意(除了非常罕见的异常(exception))。我可以看到它们已经导致你的方法成为 God Method 。下面是一个应该让您走上正轨的示例:

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();
}

关于C# Xamarin 使用了错误的 If 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27321453/

相关文章:

c# - Thread.Sleep c#.NET

c# - 在 Windows CE 6.0 中退出时终止应用程序

C#图遍历——任意两个节点之间的跟踪路径

c# - 抽象类中不需要主体的只读接口(interface)?

c# - 检查字符串是否包含具有多于 3 个小数点的 Version

javascript - 为什么在 if 语句中使用双感叹号?

jquery - 如果 sibling 有一个特定的类,则隐藏一个 div

javascript - if 语句在 javascript 中始终为真

javascript - 如果失败,则使用if/else Javascript语句禁用过渡动画

c# - 繁重的计算分析/优化