如何避免使用交换机时避免错误CS0128(在此范围中已经定义了局部变量)?

发布于 2025-01-24 01:53:12 字数 2296 浏览 0 评论 0原文

我正在学习C#,很抱歉这个愚蠢的问题。这是我的代码:

                Random prekey = new Random();

            string[] firstname = {"Александр", "Михаил", "Иван", "Максим", "Артем", "Даниил", "Дмитрий", "Кирилл", "Андрей", "Егор"};
            string[] secondname = { "Александов", "Михайлов", "Иванов", "Максимов", "Артемов", "Даниилов", "Дмитров", "Кириллов", "Андреев", "Егоров" };
            string fullname = firstname[prekey.Next(0, 10)] + " " + secondname[prekey.Next(0, 10)];
            int a = prekey.Next(0, 3);
            //string introduce;
            switch (a)
            {
                case 0:
                    string introduce = firstname[prekey.Next(0, 10)];
                        break;
                case 1:
                    string introduce = secondname[prekey.Next(0, 10)];
                        break;
                case 2:
                    string introduce = fullname;
                    break;
            }

如您所见,我的计划是将“介绍”随机介绍为“ firstName”或“第二名”或两者兼而有之。但是我面临的错误“在此范围中已经定义了名为'引入'的本地变量”。我搜索并找到了这个问题: c#:使用if/switch的错误:“此范围中已经定义的本地变量” ,但答案对我没有帮助。例如,我试图在开关之前声明一个变量,但没有任何改变。因此,我只是删除了该行作为评论。有一天,我将尝试使用“如果”,但是现在我想使用开关。我之前在这里发布了我的计算器,这里又来了:

    static void Main(string[] args)
{
    void MyMet(double? Result = null)
    {
        string[] calc = Console.ReadLine().Split(' ');

        double a = Result ?? double.Parse(calc[0]);
        char b = char.Parse(calc[1]);
        double c = double.Parse(calc[2]);

        switch (b)
        {
            case '*':
                Result = a * c;
                break;
            case '+':
                Result = a + c;
                break;
            case '-':
                Result = a - c;
                break;
            case '/':
                Result = a / c;
                break;
        }
        Console.Write(Result);
        MyMet(Result);
    }
    MyMet();
}

在这种情况下,一切都有效,但是在这里声明了一个问号(老实说,我不知道它们是如何工作的,这部分只是复制了),我尝试了要在当前情况下精确地重新创建它,但显然C#不想将字符串视为双重,因此他只是写了“ C#7.3中不可用的无用参考类型。请使用8.0或更高的语言版本。”我可以通过继续使用开关而不是切换到另一个版本来解决此问题吗?

I'm learning c# so sorry for the stupid question. Here is my code:

                Random prekey = new Random();

            string[] firstname = {"Александр", "Михаил", "Иван", "Максим", "Артем", "Даниил", "Дмитрий", "Кирилл", "Андрей", "Егор"};
            string[] secondname = { "Александов", "Михайлов", "Иванов", "Максимов", "Артемов", "Даниилов", "Дмитров", "Кириллов", "Андреев", "Егоров" };
            string fullname = firstname[prekey.Next(0, 10)] + " " + secondname[prekey.Next(0, 10)];
            int a = prekey.Next(0, 3);
            //string introduce;
            switch (a)
            {
                case 0:
                    string introduce = firstname[prekey.Next(0, 10)];
                        break;
                case 1:
                    string introduce = secondname[prekey.Next(0, 10)];
                        break;
                case 2:
                    string introduce = fullname;
                    break;
            }

As you can see, my plan is to have the "introduce" randomly be either "firstname" or "secondname" or both. But I am facing the error "A local variable named 'introduce' is already defined in this scope". I searched and found this question: C# : error using if/switch : "Local variable already defined in this scope" but the answers didn't help me. For example, I tried to declare a variable before the switch, but nothing changed. So I just removed that line as a comment. One day I will try to use "if", but now I want to use switch. I posted my calculator here before, here it is again:

    static void Main(string[] args)
{
    void MyMet(double? Result = null)
    {
        string[] calc = Console.ReadLine().Split(' ');

        double a = Result ?? double.Parse(calc[0]);
        char b = char.Parse(calc[1]);
        double c = double.Parse(calc[2]);

        switch (b)
        {
            case '*':
                Result = a * c;
                break;
            case '+':
                Result = a + c;
                break;
            case '-':
                Result = a - c;
                break;
            case '/':
                Result = a / c;
                break;
        }
        Console.Write(Result);
        MyMet(Result);
    }
    MyMet();
}

In this case, everything works, but here the variable is declared with question marks (to be honest, I have no idea how they work, this part was just copied) and I tried to recreate it exactly in the current case, but apparently C# does not wants to treat string as double, so he just wrote "Nullable reference types are not available in C# 7.3. Please use language version 8.0 or higher." Can I solve this problem by continuing to use the switch and not switching to another version?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2025-01-31 01:53:12

您需要在使用该变量之前仅声明一次。

当您分配变量(而不是声明它)时,您未指定变量名称左侧的类型。

string introduce;
switch (a)
{
    case 0:
        introduce = firstname[prekey.Next(0, 10)];
        break;
    case 1:
        introduce = secondname[prekey.Next(0, 10)];
        break;
    case 2:
        introduce = fullname;
        break;
}

You need to declare the variable only once, before it is used.

When you assign the variable (as opposed to declaring it) you do not specify the type to the left of the variable name.

string introduce;
switch (a)
{
    case 0:
        introduce = firstname[prekey.Next(0, 10)];
        break;
    case 1:
        introduce = secondname[prekey.Next(0, 10)];
        break;
    case 2:
        introduce = fullname;
        break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文