最经在改别人用QBASIC语言编写的一段程序,里面很多判断和循环语句,代码中用到很多goto语句。我用了几个成员函数进行调用,好像goto语句goto过去就不再回到程序调用处,但函数调用会回到函数调用处。不知道怎么解决,本人新手求帮助。先谢过!!部分代码如下:
public void level1540()//1540
        {
            XZ();//调用旋转函数
            if (C < D) //1550
            {
                P = J1 - Y1; Q = X1 - I1;
                P1 = I1 * P + J1 * Q; P2 = I2 * P + J2 * Q; P3 = I3 * P + J3 * Q;
                if ((P3 > P1 && P2 >= P1) || (P3 < P1 && P2 <= P1))
                {
                    P = J3 - Y1; Q = X1 - I3;
                    P1 = I3 * P + J3 * Q; P2 = I2 * P + J2 * Q; P3 = I1 * P + J1 * Q;
                    if ((P3 > P1 && P2 >= P1) || (P3 < P1 && P2 <= P1))
                    {
                        I5 = X1; J5 = Y1;
                        f = A - B;
                        return;
                    }
                    else
                    {
                        I1 = X1; J1 = Y1;
                        level1540();//goto1540
                    }
                }
                else
                {
                    I3 = X1; J3 = Y1;
                    level1540();//goto1540
                }
            }
            else
            {
                if ((P1 > P2 && P1 < S[X2, Y2]) || (P1 < P2 && P1 > S[X2, Y2]))
                {
                    I4 = X2; J4 = Y2; P = J2 - J4; Q = I4 - I2;
                    P1 = I1 * P + J1 * Q; P2 = I2 * P + J2 * Q; P3 = I3 * P + J3 * Q;
                    if ((P1 > P2 && P2 > P3) || P1 < P2 && P2 < P3)
                    {
                        f = A - B;
                        return;
                    }
                    else
                    {
                        I1 = I3; J1 = J3;
                        level1210();//goto 1210
                    }
                }
                else
                {
                    I2 = X2; J2 = Y2;
                    level1520();//goto 1520;
                }
            }
        }

解决方案 »

  1.   

    goto是终止这次变量的循环 直接跳到循环的下个变量
      

  2.   

    我说的按个关键字是continue,实在抱歉哈:)
               for (int i = 0; i < 10; i++)
                {
                    if (i == 8)
                        continue;
                    Console.WriteLine(i);
                
                }
      

  3.   

    1.goto处可以用标志跳出
    2.goto处可以用调用函数代替
      

  4.   

    摘自MSDN 补补基础知识!!!
    goto 的一个通常用法是将控制传递给特定的 switch-case 标签或 switch 语句中的默认标签。
    goto 语句还用于跳出深嵌套循环。
    下面的示例演示了 goto 在 switch 语句中的使用。
    // statements_goto_switch.cs
    using System;
    class SwitchTest
    {
        static void Main()
        {
            Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
            Console.Write("Please enter your selection: ");
            string s = Console.ReadLine();
            int n = int.Parse(s);
            int cost = 0;
            switch (n)
            {
                case 1:
                    cost += 25;
                    break;
                case 2:
                    cost += 25;
                    goto case 1;
                case 3:
                    cost += 50;
                    goto case 1;
                default:
                    Console.WriteLine("Invalid selection.");
                    break;
            }
            if (cost != 0)
            {
                Console.WriteLine("Please insert {0} cents.", cost);
            }
            Console.WriteLine("Thank you for your business.");
        }
    }
      

  5.   

    下面的示例演示了使用 goto 跳出嵌套循环。
    复制
    // statements_goto.cs
    // Nested search loops
    using System;
    public class GotoTest1
    {
        static void Main()
        {
            int x = 200, y = 4;
            int count = 0;
            string[,] array = new string[x, y];        // Initialize the array:
            for (int i = 0; i < x; i++)            for (int j = 0; j < y; j++)
                    array[i, j] = (++count).ToString();        // Read input:
            Console.Write("Enter the number to search for: ");        // Input a string:
            string myNumber = Console.ReadLine();        // Search:
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    if (array[i, j].Equals(myNumber))
                    {
                        goto Found;
                    }
                }
            }        Console.WriteLine("The number {0} was not found.", myNumber);
            goto Finish;    Found:
            Console.WriteLine("The number {0} is found.", myNumber);    Finish:
            Console.WriteLine("End of search.");
        }
    }
     
    输入
     44 
    示例输出
     Enter the number to search for: 44
    The number 44 is found.
    End of search.
      

  6.   

    哈!!!goto还是蛮有用的感觉 我之前写代码一直没用过 感觉很实在嘛!!
      

  7.   

    程序中用了很多goto,自己都有点理不清楚,我调试的结果跟原来的程序运行的结果不一致,所以不知道哪里有问题。
      

  8.   

    初步分析,你程序中goto之后是不执行下面代码的,但是你改成方法以后会执行下面的代码,所以导致的混乱  不知道我分析的靠谱不?
      

  9.   

    非常靠谱,就是你说的这个意思。原来的QBASIC程序goto非常多,看懂都花了很多时间。用方法调用,调用完成后,会再回到调用处继续执行。谢谢您
      

  10.   

    如果是这样 那你试试每个调用方法后面加一行 return; 
    return的意思就是结束此方法,应该就是你说的goto的那个意思 试试看
      

  11.   

    比如你的一个片段  
    else
       {
       I1 = I3; J1 = J3;
       level1210();//goto 1210
        return;
       }
     
      

  12.   

    你那些变量都在哪里定义的?如果都是局部变量,那可能出问题。因为qbasic里的变量都是全局变量吧。要么用全局变量(方法外面定义),要么用引用的方式传参数。
      

  13.   

    C#中可以使用goto关键字,但不是类型安全的代码。