简体   繁体   English

C#我如何结束我的GOTO语句然后继续

[英]c# how can i end my GOTO statement and go on after

I want to go somewhere in my code with the goto statement but it doesn't skip parts of my code but reads on. 我想使用goto语句进入代码中的某个位置,但是它不会跳过代码的一部分,而是继续读下去。 I just need to skip the parts i don't need... code: 我只需要跳过不需要的部分即可...代码:

   int getal1, getal2;
        int som,verschil,product,quotient;


        Console.WriteLine("first number: ");
        Console.ForegroundColor = ConsoleColor.Green;
        getal1 = Convert.ToInt32(Console.ReadLine());
        Console.ResetColor();
        Console.WriteLine("choose operator: -,+,*,/");
        if (Console.ReadLine() == "+")
        {
            goto second;
        }
        else if (Console.ReadLine() == "-")
        {
            goto second1;
        }

second:
            Console.WriteLine("Second number(+): ");
            Console.ForegroundColor = ConsoleColor.Green;
            getal2 = Convert.ToInt32(Console.ReadLine());
            Console.ResetColor();
            goto Operat;

Operat:     som = getal1 + getal2;
            verschil = getal1 - getal2;
            product = getal1 * getal2;
            quotient = getal1 / getal2;
             goto optelling;

second1:    Console.WriteLine("Second number(-): ");
            Console.ForegroundColor = ConsoleColor.Green;
            getal2 = Convert.ToInt32(Console.ReadLine());
            Console.ResetColor();
            goto Operat1;

Operat1:
            som = getal1 + getal2;
            verschil = getal1 - getal2;
            product = getal1 * getal2;
            quotient = getal1 / getal2;
            goto verschil;

optelling:
            Console.Write("De som van " );
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(getal1.ToString());
            Console.ResetColor();
            Console.Write(" en " );
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(getal2.ToString());
            Console.ResetColor();
            Console.Write(" is ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(som.ToString());
            Console.ResetColor();
            goto end;


verschil:
            Console.WriteLine("Het verschil van " + getal1.ToString() + " en " + getal2.ToString() + " is " + verschil.ToString());
goto end;

product:           
            Console.WriteLine("Het product van "  + getal1.ToString() + " en " + getal2.ToString() + " is " + product.ToString());
quotient:            
            Console.WriteLine("Het quotient van " + getal1.ToString() + " en " + getal2.ToString() + " is " + quotient.ToString());

end:
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Press Enter to continue...");
            Console.ResetColor();

            Console.readkey();

just copy paste this code and help me out of this!! 只需复制粘贴此代码,即可帮助我!!

Your fundamental problem here is that you are naively translating a batch file into C#, rather than thinking about what the batch file does and writing a program in C# that does the same thing, using the style and conventions of C#. 这里的根本问题是,您正在天真的将批处理文件转换为C#,而不是考虑批处理文件的作用,而是使用C#的样式和约定在C#中编写执行相同操作的程序。

The specific problem here is not your use of goto , but rather that every time you call Console.ReadLine it reads another line . 这里的特定问题不是您对goto的使用,而是每次调用Console.ReadLine它都会读取另一行 When you say: 当你说:

    if (Console.ReadLine() == "+")
    {
        goto second;
    }
    else if (Console.ReadLine() == "-")
    {
        goto second1;
    }

That means "if the current line is not + then check to see if the next line is - ". 这意味着“如果当前行不是+则检查下一行是否为- ”。 You meant to say 你是说

    var line = Console.ReadLine();
    if (line == "+")
    {
        goto second;
    }
    else if (line == "-")
    {
        goto second1;
    }

Whereas later in your program you correctly call Console.ReadLine to read the next line. 而稍后在程序中,您可以正确地调用Console.ReadLine以读取下一行。 Remember, every time you call Console.ReadLine in your program you are instructing the console to read the next line , not re-read the current line . 请记住, 每次在程序中调用Console.ReadLine ,都是在指示控制台读取下一行 ,而不是重新读取当前行 You seem to want it to mean one thing sometimes and another thing other times. 您似乎希望它有时表示一件事,而在其他时候表示另一件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM