【问题标题】:goto - not within scope (C#)goto - 不在范围内(C#)
【发布时间】:2018-06-10 11:34:14
【问题描述】:

我对代码很陌生。谁能以简单的方式解释为什么我不能像这样使用 goto 语句来使代码重新开始?或者,如何以正确的方式做到这一点?还有,为什么我会收到关于使用“静态”的错误消息。 ** “在 goto statmenet 范围内没有这样的标签“开始”” "修饰符 static 对此项无效"

using System;


namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Start:

            Random numberGenerator = new Random();

            int num1 = numberGenerator.Next(1,11);
            int num2 = numberGenerator.Next(1, 4);


            Console.WriteLine("What is " + num1 + " times " + num2 + "?");


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

            if (svar == num1 * num2)
            {
                Console.WriteLine("well done!");
            }
            else
            {
                int responseIndex = numberGenerator.Next(1, 4);

                switch (responseIndex)
                {
                    case 1:
                        Console.WriteLine("Wrong, try again? [Y or N]");
                        AskUser();
                        break;
                    case 2:
                        Console.WriteLine("The answer was incorrect");
                        AskUser();
                        break;
                    default:
                        Console.WriteLine("You can do better than that");
                        AskUser();
                        break;
                }



                 static void AskUser() {
                    string jaellernei = Console.ReadLine().ToUpper();
                    if (jaellernei == "Y")
                    {
                     goto Start;
                    } else
                    {
                        return;
                    } }
            }


        }
    }
}

【问题讨论】:

  • 我对代码很陌生。那么不要学习使用goto,你永远不需要在c#中使用它。
  • @Rotem 好吧,“从不”是强词......有些东西你不应该习惯性地使用,但确实有真正的用途当你确切地知道你为什么使用时它。它是我可能每年使用一次的东西......
  • @MarcGravell 在我看来,如果你足够了解使用 goto 而不会弄得一团糟,那么你知道的足以忽略我的建议。

标签: c# goto


【解决方案1】:

首先,您的 AskUser 方法错误地嵌套在另一个方法中 - 将其移出。

其次:goto 仅在单个方法中有效;您可以跳过单个堆栈帧 - 您不能在 个堆栈帧之间跳转。

第三:你应该使用goto的次数...嗯,它不是相当为零,但它渐近为零.

【讨论】:

  • 不一定是嵌套不正确。 C# 现在支持本地函数,它们只需不用static 关键字声明,即使它们是static 方法的一部分。
【解决方案2】:

除非您必须使用 goto,否则不要使用! 正如@Marc Gravell 所说,它在单个方法中有效。

或者:您可以使用 Main 方法中使用的代码创建一个方法,然后从 main 方法和使用 goto 语句的其他方法调用它。 喜欢:

 using System;
namespace ConsoleApp3
{
    class Program

    {

        static void Main(string[] args)
        {
             someFunction();
        }


        static void someFunction()
        {
            Random numberGenerator = new Random();

            int num1 = numberGenerator.Next(1, 11);
            int num2 = numberGenerator.Next(1, 4);


            Console.WriteLine("What is " + num1 + " times " + num2 + "?");


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

            if (svar == num1 * num2)
            {
                Console.WriteLine("well done!");
            }
            else
            {
                int responseIndex = numberGenerator.Next(1, 4);

                switch (responseIndex)
                {
                    case 1:
                        Console.WriteLine("Wrong, try again? [Y or N]");
                        AskUser();
                        break;
                    case 2:
                        Console.WriteLine("The answer was incorrect");
                        AskUser();
                        break;
                    default:
                        Console.WriteLine("You can do better than that");
                        AskUser();
                        break;
                }
            }
        }

        static void AskUser()
        {
            string jaellernei = Console.ReadLine().ToUpper();
            if (jaellernei == "Y")
            {
                someFunction();
            }
            else
            {
                return;
            }
        }


    }
}

【讨论】:

    【解决方案3】:

    你可以这样做

    public static Random randd = new Random();
    public static void FlachCards()
    {
    Start:
    
        if (AskAUser() == "Y")
        {
            goto Start;
        }
    
    }
    public static String AskAUser()
    {
        Console.WriteLine("Enter Y to play again");
        return Console.ReadLine();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2015-02-26
      相关资源
      最近更新 更多