最经在改别人用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;
}
}
}
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;
}
}
}
for (int i = 0; i < 10; i++)
{
if (i == 8)
continue;
Console.WriteLine(i);
}
2.goto处可以用调用函数代替
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.");
}
}
复制
// 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.
return的意思就是结束此方法,应该就是你说的goto的那个意思 试试看
else
{
I1 = I3; J1 = J3;
level1210();//goto 1210
return;
}